I
- The generic ID type, usually Long
.E
- The generic base entity type.public abstract class BaseEntityService<I extends Comparable<I> & Serializable,E extends BaseEntity<I>> extends Object
Base entity service. Let your Stateless
service classes extend from this. Ideally, you would not anymore have
the need to inject the EntityManager
in your service class and it would suffice to just delegate all
persistence actions to methods of this abstract class.
You only need to let your entities extend from one of the following mapped super classes:
BaseEntity
,
Page
,
Criteria
Modifier and Type | Class and Description |
---|---|
protected static interface |
BaseEntityService.MappedQueryBuilder<T>
Functional interface to fine-grain a JPA criteria query for any of
getPage(Page, boolean) methods taking
a specific result type, such as an entity subclass (DTO). |
protected static interface |
BaseEntityService.QueryBuilder<E>
Functional interface to fine-grain a JPA criteria query for any of
getPage(Page, boolean) methods. |
Constructor and Description |
---|
BaseEntityService()
The constructor initializes the type mapping.
|
Modifier and Type | Method and Description |
---|---|
protected Consumer<javax.persistence.EntityManager> |
afterPage()
Here you can in your DTO subclass define the callback method which needs to be invoked after any of
getPage(Page, boolean) methods is called. |
protected Consumer<javax.persistence.EntityManager> |
beforePage()
Here you can in your DTO subclass define the callback method which needs to be invoked before any of
getPage(Page, boolean) methods is called. |
protected javax.persistence.TypedQuery<E> |
createNamedQuery(String name)
Create an instance of
TypedQuery for executing a Java Persistence Query Language statement identified
by the given name. |
protected javax.persistence.TypedQuery<E> |
createQuery(String jpqlStatement)
Create an instance of
TypedQuery for executing the given Java Persistence Query Language statement. |
void |
delete(E entity)
Delete given entity.
|
protected void |
fetchLazyBlobs(E entity)
Fetch all lazy blobs of given entity.
|
protected void |
fetchLazyCollections(E entity,
Function<E,Collection<?>>... getters)
Fetch lazy collections of given entity on given getters.
|
protected void |
fetchLazyMaps(E entity,
Function<E,Map<?,?>>... getters)
Fetch lazy maps of given entity on given getters.
|
Optional<E> |
findById(I id)
Find entity by given ID.
|
List<E> |
getAll()
Get all entities.
|
E |
getById(I id)
Get entity by given ID.
|
org.omnifaces.utils.collection.PartialResultList<E> |
getPage(Page page,
boolean count)
Returns a partial result list based on given
Page . |
protected org.omnifaces.utils.collection.PartialResultList<E> |
getPage(Page page,
boolean count,
BaseEntityService.QueryBuilder<E> queryBuilder)
Returns a partial result list based on given
Page and BaseEntityService.QueryBuilder . |
protected org.omnifaces.utils.collection.PartialResultList<E> |
getPage(Page page,
boolean count,
boolean cacheable)
Returns a partial result list based on given
Page . |
protected org.omnifaces.utils.collection.PartialResultList<E> |
getPage(Page page,
boolean count,
boolean cacheable,
BaseEntityService.QueryBuilder<E> queryBuilder)
Returns a partial result list based on given
Page , entity type and BaseEntityService.QueryBuilder . |
protected <T extends E> |
getPage(Page page,
boolean count,
boolean cacheable,
Class<T> resultType,
BaseEntityService.MappedQueryBuilder<T> queryBuilder)
Returns a partial result list based on given
Page , entity type and BaseEntityService.QueryBuilder . |
protected <T extends E> |
getPage(Page page,
boolean count,
Class<T> resultType,
BaseEntityService.MappedQueryBuilder<T> mappedQueryBuilder)
Returns a partial result list based on given
Page , result type and BaseEntityService.MappedQueryBuilder . |
protected E |
manage(E entity)
Make given entity managed.
|
protected Consumer<javax.persistence.TypedQuery<?>> |
onPage(Page page,
boolean cacheable)
Here you can in your DTO subclass define the callback method which needs to be invoked when any query involved in
getPage(Page, boolean) is about to be executed. |
I |
persist(E entity)
Persist given entity.
|
void |
refresh(E entity)
Refresh given entity.
|
E |
save(E entity)
Save given entity.
|
E |
update(E entity)
Update given entity.
|
public BaseEntityService()
I
and E
will be resolved to a concrete Class<?>
.protected final javax.persistence.TypedQuery<E> createQuery(String jpqlStatement)
TypedQuery
for executing the given Java Persistence Query Language statement.jpqlStatement
- The Java Persistence Query Language statement to be executed.TypedQuery
for executing the given Java Persistence Query Language statement.protected final javax.persistence.TypedQuery<E> createNamedQuery(String name)
TypedQuery
for executing a Java Persistence Query Language statement identified
by the given name.name
- The name of the Java Persistence Query Language statement defined in metadata, which can be either
a NamedQuery
or a <persistence-unit><mapping-file>
.TypedQuery
for executing a Java Persistence Query Language statement identified
by the given name.public Optional<E> findById(I id)
id
- Entity ID to find entity for.public E getById(I id)
id
- Entity ID to get entity by.null
if there is none.public List<E> getAll()
public I persist(E entity)
entity
- Entity to persist.IllegalEntityStateException
- When entity has an ID.public E update(E entity)
entity
- Entity to update.IllegalEntityStateException
- When entity has no ID.public E save(E entity)
persist(BaseEntity)
or to update(BaseEntity)
.entity
- Entity to save.public void refresh(E entity)
entity
- Entity to refresh.IllegalEntityStateException
- When entity has no ID or has in meanwhile been deleted.public void delete(E entity)
entity
- Entity to delete.NonDeletableEntityException
- When entity has NonDeletable
annotation set.IllegalEntityStateException
- When entity has no ID or has in meanwhile been deleted.protected final E manage(E entity)
entity
- Entity to manage.IllegalEntityStateException
- When entity has no ID or has in meanwhile been deleted.@SafeVarargs protected final void fetchLazyCollections(E entity, Function<E,Collection<?>>... getters)
PluralAttribute
not of type PluralAttribute.CollectionType.MAP
.
Note that the implementation does for simplicitly not check if those are actually lazy or eager.entity
- Entity instance to fetch lazy collections on.getters
- Getters of those lazy collections.@SafeVarargs protected final void fetchLazyMaps(E entity, Function<E,Map<?,?>>... getters)
PluralAttribute
of type PluralAttribute.CollectionType.MAP
.
Note that the implementation does for simplicitly not check if those are actually lazy or eager.entity
- Entity instance to fetch lazy maps on.getters
- Getters of those lazy collections.protected final void fetchLazyBlobs(E entity)
entity
- Entity instance to fetch all blobs on.protected Consumer<javax.persistence.EntityManager> beforePage()
getPage(Page, boolean)
methods is called. For example, to set a vendor specific EntityManager
hint.
The default implementation returns a no-op callback.getPage(Page, boolean)
methods is called.protected Consumer<javax.persistence.TypedQuery<?>> onPage(Page page, boolean cacheable)
getPage(Page, boolean)
is about to be executed. For example, to set a vendor specific Query
hint.
The default implementation sets the Hibernate cacheable
and cacheRegion
hints.page
- The page on which this query is based.cacheable
- Whether the results should be cacheable.getPage(Page, boolean)
is about
to be executed.protected Consumer<javax.persistence.EntityManager> afterPage()
getPage(Page, boolean)
methods is called. For example, to remove a vendor specific EntityManager
hint.
The default implementation returns a no-op callback.getPage(Page, boolean)
methods is called.public org.omnifaces.utils.collection.PartialResultList<E> getPage(Page page, boolean count)
Page
. This will by default cache the results.page
- The page to return a partial result list for.count
- Whether to run the COUNT(id)
query to estimate total number of results. This will be
available by PartialResultList.getEstimatedTotalNumberOfResults()
.Page
.protected org.omnifaces.utils.collection.PartialResultList<E> getPage(Page page, boolean count, boolean cacheable)
Page
.page
- The page to return a partial result list for.count
- Whether to run the COUNT(id)
query to estimate total number of results. This will be
available by PartialResultList.getEstimatedTotalNumberOfResults()
.cacheable
- Whether the results should be cacheable.Page
.protected org.omnifaces.utils.collection.PartialResultList<E> getPage(Page page, boolean count, BaseEntityService.QueryBuilder<E> queryBuilder)
Page
and BaseEntityService.QueryBuilder
. This will by default cache
the results.page
- The page to return a partial result list for.count
- Whether to run the COUNT(id)
query to estimate total number of results. This will be
available by PartialResultList.getEstimatedTotalNumberOfResults()
.queryBuilder
- This allows fine-graining the JPA criteria query.Page
and BaseEntityService.QueryBuilder
.protected org.omnifaces.utils.collection.PartialResultList<E> getPage(Page page, boolean count, boolean cacheable, BaseEntityService.QueryBuilder<E> queryBuilder)
Page
, entity type and BaseEntityService.QueryBuilder
.page
- The page to return a partial result list for.count
- Whether to run the COUNT(id)
query to estimate total number of results. This will be
available by PartialResultList.getEstimatedTotalNumberOfResults()
.cacheable
- Whether the results should be cacheable.queryBuilder
- This allows fine-graining the JPA criteria query.Page
and BaseEntityService.QueryBuilder
.protected <T extends E> org.omnifaces.utils.collection.PartialResultList<T> getPage(Page page, boolean count, Class<T> resultType, BaseEntityService.MappedQueryBuilder<T> mappedQueryBuilder)
Page
, result type and BaseEntityService.MappedQueryBuilder
. This will
by default cache the results.T
- The generic type of the entity or a DTO subclass thereof.page
- The page to return a partial result list for.count
- Whether to run the COUNT(id)
query to estimate total number of results. This will be
available by PartialResultList.getEstimatedTotalNumberOfResults()
.resultType
- The result type which can be the entity type itself or a DTO subclass thereof.mappedQueryBuilder
- This allows fine-graining the JPA criteria query and must return a mapping of
getters-paths.Page
and BaseEntityService.MappedQueryBuilder
.IllegalArgumentException
- When the result type does not equal entity type and mapping is empty.protected <T extends E> org.omnifaces.utils.collection.PartialResultList<T> getPage(Page page, boolean count, boolean cacheable, Class<T> resultType, BaseEntityService.MappedQueryBuilder<T> queryBuilder)
Page
, entity type and BaseEntityService.QueryBuilder
.T
- The generic type of the entity or a DTO subclass thereof.page
- The page to return a partial result list for.count
- Whether to run the COUNT(id)
query to estimate total number of results. This will be
available by PartialResultList.getEstimatedTotalNumberOfResults()
.cacheable
- Whether the results should be cacheable.resultType
- The result type which can be the entity type itself or a DTO subclass thereof.queryBuilder
- This allows fine-graining the JPA criteria query and must return a mapping of
getters-paths when result type does not equal entity type.Page
and BaseEntityService.MappedQueryBuilder
.IllegalArgumentException
- When the result type does not equal entity type and mapping is empty.Copyright © 2015–2017 OmniFaces. All rights reserved.