Class GenericDAOImpl<T,ID>

java.lang.Object
com.alpaca.persistence.impl.GenericDAOImpl<T,ID>
Type Parameters:
T - entity type managed by this DAO
ID - type of the entity's identifier
All Implemented Interfaces:
IGenericDAO<T,ID>
Direct Known Subclasses:
AdvertiserDAOImpl, PermissionDAOImpl, ProfileDAOImpl, RoleDAOImpl, UserDAOImpl

public abstract class GenericDAOImpl<T,ID> extends Object implements IGenericDAO<T,ID>
Abstract base implementation of IGenericDAO, providing generic CRUD and pagination operations for any entity type T with identifier type ID.

Concrete DAO implementations must supply the specific GenericRepo and entity class by implementing the abstract getRepo() and getEntity() methods.

This class leverages Spring Data repositories to delegate standard persistence operations in a type-safe, reusable fashion.

  • Constructor Details

    • GenericDAOImpl

      public GenericDAOImpl()
  • Method Details

    • getRepo

      protected abstract GenericRepo<T,ID> getRepo()
      Supplies the Spring Data repository for the specific entity type.
      Returns:
      the repository instance for T
    • getEntity

      protected abstract Class<T> getEntity()
      Provides the entity class handled by this DAO implementation.
      Returns:
      the Class object representing T
    • findById

      public Optional<T> findById(ID id)
      Finds an entity by its identifier.
      Specified by:
      findById in interface IGenericDAO<T,ID>
      Parameters:
      id - the identifier of the entity to find; may be null
      Returns:
      an Optional containing the entity if found, otherwise empty
    • findAllByIds

      public List<T> findAllByIds(Collection<ID> ids)
      Retrieves all entities matching the provided collection of identifiers.
      Specified by:
      findAllByIds in interface IGenericDAO<T,ID>
      Parameters:
      ids - the collection of identifiers; may be null or empty
      Returns:
      a List of entities found; empty if none match
    • deleteById

      public void deleteById(ID id)
      Deletes the entity identified by the given ID, if it exists.
      Specified by:
      deleteById in interface IGenericDAO<T,ID>
      Parameters:
      id - the identifier of the entity to delete; may be null
    • save

      public T save(T t)
      Saves or updates the given entity.
      Specified by:
      save in interface IGenericDAO<T,ID>
      Parameters:
      t - the entity to save; must not be null
      Returns:
      the saved entity instance
    • saveAll

      public List<T> saveAll(Collection<T> t)
      Saves or updates a collection of entities.
      Specified by:
      saveAll in interface IGenericDAO<T,ID>
      Parameters:
      t - the collection of entities to save; must not be null
      Returns:
      a List of saved entity instances
    • findAll

      public List<T> findAll()
      Retrieves all entities of type T.
      Specified by:
      findAll in interface IGenericDAO<T,ID>
      Returns:
      a List of all entities; empty if none exist
    • findAllPage

      public org.springframework.data.domain.Page<T> findAllPage(org.springframework.data.domain.Pageable pageable)
      Retrieves entities in a paginated fashion using the provided Pageable.
      Specified by:
      findAllPage in interface IGenericDAO<T,ID>
      Parameters:
      pageable - pagination and sorting parameters; must not be null
      Returns:
      a Page of entities
    • existsById

      public boolean existsById(ID id)
      Checks whether an entity exists with the given identifier.
      Specified by:
      existsById in interface IGenericDAO<T,ID>
      Parameters:
      id - the identifier to check; may be null
      Returns:
      true if an entity exists with the specified ID; false otherwise
    • existsAllByIds

      public boolean existsAllByIds(Collection<ID> ids)
      Verifies whether all entities corresponding to the provided identifiers exist.
      Specified by:
      existsAllByIds in interface IGenericDAO<T,ID>
      Parameters:
      ids - the collection of IDs to check; may be null
      Returns:
      true if the count of matching entities equals the number of IDs provided; false otherwise