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.FINER
will log the getPage(Page, boolean)
arguments, the set parameter values and the full query result.
Level.FINE
will log computed type mapping (the actual values of I
and E
type paramters), and
any discovered ElementCollection
, ManyToOne
, OneToOne
and OneToMany
mappings of the entity. This is
internally used in order to be able to build proper queries to perform a search inside those fields.
Level.WARNING
will log unparseable or illegal criteria values. The BaseEntityService
will skip them and continue.
Level.SEVERE
will log constraint violations wrapped in any ConstraintViolationException
during
persist(BaseEntity)
and update(BaseEntity)
. Due to technical limitations, it will during update()
only
happen when javax.persistence.validation.mode
property in persistence.xml
is explicitly set to
CALLBACK
(and thus not to its default of AUTO
).
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<EntityManager> |
afterPage()
Here you can in your
BaseEntityService subclass define the callback method which needs to be invoked after any of
getPage(Page, boolean) methods is called. |
protected Consumer<EntityManager> |
beforePage()
Here you can in your
BaseEntityService 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 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 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 boolean |
exists(E entity)
Check whether given entity exists.
|
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 EntityManager |
getEntityManager()
Returns the entity manager being used.
|
protected EntityType<E> |
getMetamodel()
Returns the metamodel of current base entity.
|
<T extends Comparable<T> & Serializable> |
getMetamodel(BaseEntity<T> entity)
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<TypedQuery<?>> |
onPage(Page page,
boolean cacheable)
Here you can in your
BaseEntityService 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()
.protected EntityType<E> getMetamodel()
public <T extends Comparable<T> & Serializable> EntityType<BaseEntity<T>> getMetamodel(BaseEntity<T> entity)
T
- The generic type of the entity or a DTO subclass thereof.entity
- Base entity to obtain metamodel for.protected 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 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 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.protected boolean exists(E entity)
entity
- Entity to check.public List<E> getAll()
public I persist(E entity)
entity
- Entity to persist.IllegalEntityStateException
- When entity is already persisted or its ID is not generated.public E update(E entity)
javax.persistence.validation.mode
property in persistence.xml
is explicitly set
to CALLBACK
(and thus not to its default of AUTO
), then any bean validation constraint violation will be
logged separately. Due to technical limitations, this effectively means that bean validation is invoked twice. First in this method
in order to be able to obtain the constraint violations and then once more while JTA is committing the transaction, but is executed
beyond the scope of this method.entity
- Entity to update.IllegalEntityStateException
- When entity is not persisted or its ID is not generated.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.IllegalEntityStateException
- When ID of entity is not generated.public void reset(E entity)
entity
- Entity to reset.IllegalEntityStateException
- When entity has no ID.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.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.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.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<EntityManager> beforePage()
BaseEntityService
subclass define the callback method which needs to be invoked before any of
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<TypedQuery<?>> onPage(Page page, boolean cacheable)
BaseEntityService
subclass define the callback method which needs to be invoked when any query involved in
getPage(Page, boolean)
is about to be executed. For example, to set a vendor specific Query
hint.
The default implementation sets Hibernate, EclipseLink and JPA 2.0 cache-related hints. When cacheable
argument is
true
, then it reads from cache where applicable, else it will read from DB and force a refresh of cache. Note that
this is not supported by OpenJPA. Additionally, the default implementation sets Hibernate cache region identifier to
Page.toString()
.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<EntityManager> afterPage()
BaseEntityService
subclass define the callback method which needs to be invoked after any of
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–2018 OmniFaces. All rights reserved.