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:
BaseEntityService
uses JULI Logger
for logging.
Level.WARNING
will log unparseable or illegal criteria values. The BaseEntityService
will skip them and continue.
Level.FINE
will log computed type mapping (the actual values of I
and E
type paramters), and
any discovered ElementCollection
and OneToMany
mappings of the entity. This is internally used in order to be able
to build proper queries to perform a search inside a ElementCollection
or OneToMany
field.
Level.FINER
will log the getPage(Page, boolean)
arguments, the set parameter values and the full query result.
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 long |
countForeignKeyReferencesTo(E entity)
Returns count of all foreign key references to given entity.
|
protected javax.persistence.Query |
createNamedQuery(String name)
Create an instance of
Query for executing a Java Persistence Query Language statement identified
by the given name, usually to perform an INSERT, UPDATE or DELETE. |
protected javax.persistence.TypedQuery<E> |
createNamedTypedQuery(String name)
Create an instance of
TypedQuery for executing a Java Persistence Query Language statement identified
by the given name, usually to perform a SELECT. |
void |
delete(E entity)
Delete given entity.
|
void |
delete(Iterable<E> entities)
Delete given entities.
|
protected E |
fetchLazyBlobs(E entity)
Fetch all lazy blobs of given entity.
|
protected E |
fetchLazyCollections(E entity,
Function<E,Collection<?>>... getters)
Fetch lazy collections of given entity on given getters.
|
protected E |
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.
|
static BaseEntityService<?,?> |
getCurrentInstance()
Returns the currently active
BaseEntityService from the SessionContext . |
Database |
getDatabase()
Returns the SQL database being used.
|
protected javax.persistence.EntityManager |
getEntityManager()
Returns the entity manager being used.
|
javax.persistence.metamodel.EntityType<? extends BaseEntity> |
getMetamodel(Class<? extends BaseEntity> type)
Returns the metamodel of given base entity.
|
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 . |
Provider |
getProvider()
Returns the JPA provider being used.
|
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 |
reset(E entity)
Reset given entity.
|
E |
save(E entity)
Save given entity.
|
E |
update(E entity)
Update given entity.
|
List<E> |
update(Iterable<E> entities)
Update given entities.
|
public BaseEntityService()
I
and E
will be resolved to a concrete Class<?>
.public Provider getProvider()
getEntityManager()
.public Database getDatabase()
getEntityManager()
.public javax.persistence.metamodel.EntityType<? extends BaseEntity> getMetamodel(Class<? extends BaseEntity> type)
protected javax.persistence.EntityManager getEntityManager()
BaseEntityService
like below
wherein you supply the persistence unit specific entity manager and then let all your service classes extend
from it instead.
public abstract class YourBaseEntityService<E extends BaseEntity<Long>> extends BaseEntityService<Long, E> { @PersistenceContext(unitName = "yourPersistenceUnitName") private EntityManager entityManager; @Override public EntityManager getEntityManager() { return entityManager; } }
protected javax.persistence.TypedQuery<E> createNamedTypedQuery(String name)
TypedQuery
for executing a Java Persistence Query Language statement identified
by the given name, usually to perform a SELECT.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, usually to perform a SELECT.protected javax.persistence.Query createNamedQuery(String name)
Query
for executing a Java Persistence Query Language statement identified
by the given name, usually to perform an INSERT, UPDATE or DELETE.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>
.Query
for executing a Java Persistence Query Language statement identified
by the given name, usually to perform an INSERT, UPDATE or DELETE.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 List<E> update(Iterable<E> entities)
entities
- Entities to update.IllegalEntityStateException
- When at least one entity has no ID.public E save(E entity)
persist(BaseEntity)
or to update(BaseEntity)
.entity
- Entity to save.public void reset(E entity)
entity
- Entity to reset.IllegalEntityStateException
- When entity has no ID.javax.persistence.EntityNotFoundException
- When entity 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.javax.persistence.EntityNotFoundException
- When entity has in meanwhile been deleted.public void delete(Iterable<E> entities)
entities
- Entities to delete.NonDeletableEntityException
- When at least one entity has NonDeletable
annotation set.IllegalEntityStateException
- When at least one entity has no ID.javax.persistence.EntityNotFoundException
- When at least one entity has in meanwhile been deleted.protected E manage(E entity)
entity
- Entity to manage.IllegalEntityStateException
- When entity has no ID.javax.persistence.EntityNotFoundException
- When entity has in meanwhile been deleted.protected long countForeignKeyReferencesTo(E entity)
entity
- Entity to count all foreign key references for.protected E 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.protected E 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 E 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.public static BaseEntityService<?,?> getCurrentInstance()
BaseEntityService
from the SessionContext
.BaseEntityService
from the SessionContext
.IllegalStateException
- if there is none, which can happen if this method is called outside EJB context,
or when currently invoked EJB service is not an instance of BaseEntityService
.Copyright © 2015–2017 OmniFaces. All rights reserved.