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 typeU
- the upsert/patch request DTO typeR
- the full response DTO typeF
- the reference DTO typeID
- 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
FieldsModifier and TypeFieldDescriptionprotected static final String
The attribute name for the ID field in the entity.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.The reference class type, used for lightweight projections.The JPA repository used for CRUD operations.The full response class type, used for detailed entity projections. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
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 TypeMethodDescriptionprotected void
applyRowSecurity
(T entity) Apply row-security validation/mutation to the given entity prior to persistence.protected org.springframework.data.jpa.domain.Specification
<T> Create a Specification to filter by ID.long
count()
Count total number of entities.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 an entity by ID, invoking pre/post-delete hooks.void
deleteAllByIds
(Collection<ID> ids) Bulk delete entities by their IDs.boolean
existsById
(ID id) Check if an entity exists by ID.org.springframework.data.domain.Page
<R> Retrieve a paginated list of DTO projections, optionally filtered by a search query.Find an entity by ID or throw ResourceNotFoundException.findByIdOptional
(ID id) Find an entity by ID, returning Optional.findByIds
(Collection<ID> ids) Retrieve entities by a collection of IDs.findReferenceById
(ID id) Return a reference proxy to the entity without hitting the database immediately.protected T
loadEntity
(ID id) Load the entity by ID applying row-security; throws if not found.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
Post-save hook for custom logic after an entity is saved.protected void
Pre-delete hook for custom logic before an entity is deleted.protected void
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 aRowSecurityHandler
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 an existing entity by ID, invoking pre-/post-update hooks.updateAll
(List<Identified<ID, U>> requests) Bulk update entities by their IDs.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.
-
Field Details
-
repository
The JPA repository used for CRUD operations. This is the primary interface for interacting with the database. -
mapper
The mapper used to convert between entity and DTOs. This handles the conversion logic for create, update, and response DTOs. -
entityClass
-
responseClass
The full response class type, used for detailed entity projections. This is typically used for API responses that require all entity details. -
refClass
The reference class type, used for lightweight projections. This is useful for scenarios where full entity details are not required. -
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
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 operationsmapper
- the entity mapper for converting between entity and DTOsentityClass
- the JPA entity class typeresponseClass
- the full response DTO class typerefClass
- 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. -
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. -
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. -
rowSecurityFilter
Override to provide row-level security filter. -
rowSecurityHandlers
Override to provide aRowSecurityHandler
used for both filtering and write-time validation. -
applyRowSecurity
Apply row-security validation/mutation to the given entity prior to persistence. -
findByIds
Retrieve entities by a collection of IDs. -
findByIdOptional
Find an entity by ID, returning Optional.- Specified by:
findByIdOptional
in interfaceCrudService<T,
U, R, F, ID> - Parameters:
id
- identifier- Returns:
- Optional containing entity if found
-
findById
Find an entity by ID or throw ResourceNotFoundException.- Specified by:
findById
in interfaceCrudService<T,
U, R, F, ID> - Parameters:
id
- identifier- Returns:
- found entity
- Throws:
ResourceNotFoundException
- if not found
-
findReferenceById
Return a reference proxy to the entity without hitting the database immediately.- Specified by:
findReferenceById
in interfaceCrudService<T,
U, R, F, ID> - Parameters:
id
- identifier- Returns:
- entity reference proxy
-
loadEntity
Load the entity by ID applying row-security; throws if not found. -
create
Create a new entity from the request DTO, invoking pre-/post-save hooks. -
update
Update an existing entity by ID, invoking pre-/post-update hooks. -
patch
Partially update an existing entity (PATCH semantics). Override prePatch/postPatch for custom logic. -
upsert
Create or update an entity based on presence/existence of ID in request (UPSERT). -
createAll
Bulk create entities from a collection of request DTOs. -
upsertAll
Bulk upsert: create or update for each request in the batch. -
updateAll
Bulk update entities by their IDs. -
patchAll
Bulk patch entities by their IDs. -
delete
Delete an entity by ID, invoking pre/post-delete hooks. -
deleteAllByIds
Bulk delete entities by their IDs.- Specified by:
deleteAllByIds
in interfaceCrudService<T,
U, R, F, ID> - Parameters:
ids
- collection of identifiers to delete
-
existsById
Check if an entity exists by ID.- Specified by:
existsById
in interfaceCrudService<T,
U, R, F, ID> - Parameters:
id
- identifier- Returns:
- true if exists
-
count
public long count()Count total number of entities. -
byId
Create a Specification to filter by ID.- Parameters:
id
- identifier- Returns:
- Specification for the given ID
-
preSave
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 savedrequest
- the request DTO containing creation or update data
-
postSave
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
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
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
-