Class AbstractCrudService<T,U,R,F,ID>

java.lang.Object
nl.datasteel.crudcraft.runtime.service.AbstractCrudService<T,U,R,F,ID>
Type Parameters:
T - the JPA entity type
U - the upsert/patch request DTO type
R - the full response DTO type
F - the reference DTO type
ID - the identifier type (e.g., UUID, Long)
All Implemented Interfaces:
CrudService<T,U,R,F,ID>

public abstract class AbstractCrudService<T,U,R,F,ID> extends Object implements CrudService<T,U,R,F,ID>
Generic abstract base for CRUD operations, with hooks for custom business logic, bulk operations, upsert, and partial updates (patch).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Class<T>
     
    protected static final String
    The attribute name for the ID field in the entity.
    protected final EntityMapper<T,U,R,F,ID>
    The mapper used to convert between entity and DTOs.
    protected final QueryExecutionStrategy<T>
    The strategy used to execute queries, either via QueryDSL or JPA Specifications.
    protected final Class<F>
    The reference class type, used for lightweight projections.
    protected final org.springframework.data.jpa.repository.JpaRepository<T,ID>
    The JPA repository used for CRUD operations.
    protected final Class<R>
    The full response class type, used for detailed entity projections.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractCrudService(org.springframework.data.jpa.repository.JpaRepository<T,ID> repository, EntityMapper<T,U,R,F,ID> mapper, Class<T> entityClass, Class<R> responseClass, Class<F> refClass)
    Constructor to initialize the service with repository, mapper, and entity classes.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Apply row-security validation/mutation to the given entity prior to persistence.
    protected org.springframework.data.jpa.domain.Specification<T>
    byId(ID id)
    Create a Specification to filter by ID.
    long
    Count total number of entities.
    create(U request)
    Create a new entity from the request DTO, invoking pre-/post-save hooks.
    createAll(Collection<U> requests)
    Bulk create entities from a collection of request DTOs.
    void
    delete(ID id)
    Delete an entity by ID, invoking pre/post-delete hooks.
    void
    Bulk delete entities by their IDs.
    boolean
    Check if an entity exists by ID.
    org.springframework.data.domain.Page<R>
    findAll(org.springframework.data.domain.Pageable pageable, String searchQuery)
    Retrieve a paginated list of DTO projections, optionally filtered by a search query.
    Find an entity by ID or throw ResourceNotFoundException.
    Find an entity by ID, returning Optional.
    Retrieve entities by a collection of IDs.
    Return a reference proxy to the entity without hitting the database immediately.
    protected T
    Load the entity by ID applying row-security; throws if not found.
    patch(ID id, U request)
    Partially update an existing entity (PATCH semantics).
    patchAll(List<Identified<ID,U>> requests)
    Bulk patch entities by their IDs.
    protected void
    postDelete(T entity)
    Post-delete hook for custom logic after an entity is deleted.
    protected void
    postSave(T entity)
    Post-save hook for custom logic after an entity is saved.
    protected void
    preDelete(T entity)
    Pre-delete hook for custom logic before an entity is deleted.
    protected void
    preSave(T entity, U request)
    Pre-save hook for custom logic before an entity is saved.
    protected org.springframework.data.jpa.domain.Specification<T>
    Override to provide row-level security filter.
    protected List<RowSecurityHandler<?>>
    Override to provide a RowSecurityHandler used for both filtering and write-time validation.
    protected com.querydsl.core.types.Predicate
    Override to provide row-level security filter using QueryDSL.
    org.springframework.data.domain.Page<R>
    search(SearchRequest<T> request, org.springframework.data.domain.Pageable pageable)
    Retrieve a paginated list of DTO projections, optionally filtered by a search request.
    org.springframework.data.domain.Page<F>
    searchRef(SearchRequest<T> request, org.springframework.data.domain.Pageable pageable)
    Retrieve a paginated list of reference DTO projections, optionally filtered by a search request.
    update(ID id, U request)
    Update an existing entity by ID, invoking pre-/post-update hooks.
    Bulk update entities by their IDs.
    upsert(U request)
    Create or update an entity based on presence/existence of ID in request (UPSERT).
    upsertAll(Collection<U> requests)
    Bulk upsert: create or update for each request in the batch.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • repository

      protected final org.springframework.data.jpa.repository.JpaRepository<T,ID> repository
      The JPA repository used for CRUD operations. This is the primary interface for interacting with the database.
    • mapper

      protected final EntityMapper<T,U,R,F,ID> mapper
      The mapper used to convert between entity and DTOs. This handles the conversion logic for create, update, and response DTOs.
    • entityClass

      protected final Class<T> entityClass
    • responseClass

      protected final Class<R> responseClass
      The full response class type, used for detailed entity projections. This is typically used for API responses that require all entity details.
    • refClass

      protected final Class<F> refClass
      The reference class type, used for lightweight projections. This is useful for scenarios where full entity details are not required.
    • queryExecutor

      protected final QueryExecutionStrategy<T> queryExecutor
      The strategy used to execute queries, either via QueryDSL or JPA Specifications. This allows for flexible query execution based on the repository capabilities.
    • ID_ATTRIBUTE

      protected static final String ID_ATTRIBUTE
      The attribute name for the ID field in the entity. This is used to construct predicates.
      See Also:
  • Constructor Details

    • AbstractCrudService

      protected AbstractCrudService(org.springframework.data.jpa.repository.JpaRepository<T,ID> repository, EntityMapper<T,U,R,F,ID> mapper, Class<T> entityClass, Class<R> responseClass, Class<F> refClass)
      Constructor to initialize the service with repository, mapper, and entity classes.
      Parameters:
      repository - the JPA repository for CRUD operations
      mapper - the entity mapper for converting between entity and DTOs
      entityClass - the JPA entity class type
      responseClass - the full response DTO class type
      refClass - the reference DTO class type
  • Method Details

    • rowSecurityPredicate

      protected com.querydsl.core.types.Predicate rowSecurityPredicate()
      Override to provide row-level security filter using QueryDSL.
    • findAll

      public org.springframework.data.domain.Page<R> findAll(org.springframework.data.domain.Pageable pageable, String searchQuery)
      Retrieve a paginated list of DTO projections, optionally filtered by a search query.
      Specified by:
      findAll in interface CrudService<T,U,R,F,ID>
      Parameters:
      pageable - pagination information
      searchQuery - optional search string
      Returns:
      page of DTOs matching criteria
    • search

      public org.springframework.data.domain.Page<R> search(SearchRequest<T> request, org.springframework.data.domain.Pageable pageable)
      Retrieve a paginated list of DTO projections, optionally filtered by a search request.
      Specified by:
      search in interface CrudService<T,U,R,F,ID>
      Parameters:
      request - the search request containing criteria
      pageable - pagination information
      Returns:
      page of DTOs matching criteria
    • searchRef

      public org.springframework.data.domain.Page<F> searchRef(SearchRequest<T> request, org.springframework.data.domain.Pageable pageable)
      Retrieve a paginated list of reference DTO projections, optionally filtered by a search request.
      Specified by:
      searchRef in interface CrudService<T,U,R,F,ID>
      Parameters:
      request - the search request containing criteria
      pageable - pagination information
      Returns:
      page of reference DTOs matching criteria
    • rowSecurityFilter

      protected org.springframework.data.jpa.domain.Specification<T> rowSecurityFilter()
      Override to provide row-level security filter.
    • rowSecurityHandlers

      protected List<RowSecurityHandler<?>> rowSecurityHandlers()
      Override to provide a RowSecurityHandler used for both filtering and write-time validation.
    • applyRowSecurity

      protected void applyRowSecurity(T entity)
      Apply row-security validation/mutation to the given entity prior to persistence.
    • findByIds

      public List<R> findByIds(Collection<ID> ids)
      Retrieve entities by a collection of IDs.
      Specified by:
      findByIds in interface CrudService<T,U,R,F,ID>
      Parameters:
      ids - collection of identifiers
      Returns:
      list of entities with matching IDs
    • findByIdOptional

      public Optional<R> findByIdOptional(ID id)
      Find an entity by ID, returning Optional.
      Specified by:
      findByIdOptional in interface CrudService<T,U,R,F,ID>
      Parameters:
      id - identifier
      Returns:
      Optional containing entity if found
    • findById

      public R findById(ID id)
      Find an entity by ID or throw ResourceNotFoundException.
      Specified by:
      findById in interface CrudService<T,U,R,F,ID>
      Parameters:
      id - identifier
      Returns:
      found entity
      Throws:
      ResourceNotFoundException - if not found
    • findReferenceById

      public T findReferenceById(ID id)
      Return a reference proxy to the entity without hitting the database immediately.
      Specified by:
      findReferenceById in interface CrudService<T,U,R,F,ID>
      Parameters:
      id - identifier
      Returns:
      entity reference proxy
    • loadEntity

      protected T loadEntity(ID id)
      Load the entity by ID applying row-security; throws if not found.
    • create

      public R create(U request)
      Create a new entity from the request DTO, invoking pre-/post-save hooks.
      Specified by:
      create in interface CrudService<T,U,R,F,ID>
      Parameters:
      request - DTO containing creation data
      Returns:
      created DTO
    • update

      public R update(ID id, U request)
      Update an existing entity by ID, invoking pre-/post-update hooks.
      Specified by:
      update in interface CrudService<T,U,R,F,ID>
      Parameters:
      id - identifier of the entity to update
      request - DTO containing updated data
      Returns:
      updated DTO
    • patch

      public R patch(ID id, U request)
      Partially update an existing entity (PATCH semantics). Override prePatch/postPatch for custom logic.
      Specified by:
      patch in interface CrudService<T,U,R,F,ID>
      Parameters:
      id - identifier of the entity to patch
      request - DTO containing only fields to update
      Returns:
      patched DTO
    • upsert

      public R upsert(U request)
      Create or update an entity based on presence/existence of ID in request (UPSERT).
      Specified by:
      upsert in interface CrudService<T,U,R,F,ID>
      Parameters:
      request - DTO containing upsert data (must carry ID when updating)
      Returns:
      created or updated DTO
    • createAll

      public List<R> createAll(Collection<U> requests)
      Bulk create entities from a collection of request DTOs.
      Specified by:
      createAll in interface CrudService<T,U,R,F,ID>
      Parameters:
      requests - collection of creation DTOs
      Returns:
      list of created DTOs
    • upsertAll

      public List<R> upsertAll(Collection<U> requests)
      Bulk upsert: create or update for each request in the batch.
      Specified by:
      upsertAll in interface CrudService<T,U,R,F,ID>
      Parameters:
      requests - collection of upsert DTOs
      Returns:
      list of created or updated DTOs
    • updateAll

      public List<R> updateAll(List<Identified<ID,U>> requests)
      Bulk update entities by their IDs.
      Specified by:
      updateAll in interface CrudService<T,U,R,F,ID>
      Parameters:
      requests - collection of update DTOs
      Returns:
      list of updated DTOs
    • patchAll

      public List<R> patchAll(List<Identified<ID,U>> requests)
      Bulk patch entities by their IDs.
      Specified by:
      patchAll in interface CrudService<T,U,R,F,ID>
      Parameters:
      requests - collection of patch DTOs
      Returns:
      list of patched DTOs
    • delete

      public void delete(ID id)
      Delete an entity by ID, invoking pre/post-delete hooks.
      Specified by:
      delete in interface CrudService<T,U,R,F,ID>
      Parameters:
      id - identifier of entity to delete
    • deleteAllByIds

      public void deleteAllByIds(Collection<ID> ids)
      Bulk delete entities by their IDs.
      Specified by:
      deleteAllByIds in interface CrudService<T,U,R,F,ID>
      Parameters:
      ids - collection of identifiers to delete
    • existsById

      public boolean existsById(ID id)
      Check if an entity exists by ID.
      Specified by:
      existsById in interface CrudService<T,U,R,F,ID>
      Parameters:
      id - identifier
      Returns:
      true if exists
    • count

      public long count()
      Count total number of entities.
      Specified by:
      count in interface CrudService<T,U,R,F,ID>
      Returns:
      count of entities
    • byId

      protected org.springframework.data.jpa.domain.Specification<T> byId(ID id)
      Create a Specification to filter by ID.
      Parameters:
      id - identifier
      Returns:
      Specification for the given ID
    • preSave

      protected void preSave(T entity, U request)
      Pre-save hook for custom logic before an entity is saved. This is called before the entity is persisted to the repository.
      Parameters:
      entity - the entity to be saved
      request - the request DTO containing creation or update data
    • postSave

      protected void postSave(T entity)
      Post-save hook for custom logic after an entity is saved. This is called after the entity has been persisted to the repository.
      Parameters:
      entity - the entity that was saved
    • preDelete

      protected void preDelete(T entity)
      Pre-delete hook for custom logic before an entity is deleted. This is called before the entity is removed from the repository.
      Parameters:
      entity - the entity that will be deleted
    • postDelete

      protected void postDelete(T entity)
      Post-delete hook for custom logic after an entity is deleted. This is called after the entity has been removed from the repository.
      Parameters:
      entity - the entity that was deleted