ID
- is the generic type if the primary key
.E
- is the generic type of the managed PersistenceEntity
.public abstract class AbstractGenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>> extends Object implements GenericDao<ID,E>
GenericDao
interface.Modifier and Type | Field and Description |
---|---|
private javax.persistence.EntityManager |
entityManager |
private static org.slf4j.Logger |
LOG
Logger instance.
|
Constructor and Description |
---|
AbstractGenericDao()
The constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
applyCriteria(net.sf.mmm.util.search.base.AbstractSearchCriteria criteria,
com.mysema.query.jpa.impl.JPAQuery query)
Applies the meta-data of the given
search criteria to the given JPAQuery . |
protected void |
applyCriteria(net.sf.mmm.util.search.base.AbstractSearchCriteria criteria,
javax.persistence.Query query)
Applies the meta-data of the given
search criteria to the given Query . |
protected void |
applyCriteria(SearchCriteriaTo criteria,
com.mysema.query.jpa.impl.JPAQuery query)
Applies the meta-data of the given
search criteria to the given Query . |
protected void |
applyPagination(PaginationTo pagination,
com.mysema.query.jpa.impl.JPAQuery query)
Applies the
pagination criteria to the given JPAQuery . |
protected Long |
calculateTotalBeforePagination(PaginationTo pagination,
com.mysema.query.jpa.impl.JPAQuery query)
Calculates the total number of entities the given
query would return without pagination applied. |
protected PaginationResultTo |
createPaginationResult(PaginationTo pagination,
com.mysema.query.jpa.impl.JPAQuery query)
|
void |
delete(E entity)
Deletes a given entity.
|
void |
delete(ID id)
Deletes the entity with the given id.
|
void |
delete(Iterable<? extends E> entities)
Deletes the given entities.
|
boolean |
exists(ID id)
Returns whether an entity with the given id exists.
|
E |
find(ID id)
Retrieves an entity by its id.
|
protected List<E> |
findAll() |
List<E> |
findAll(Iterable<ID> ids)
Returns all instances of the type with the given IDs.
|
E |
findOne(ID id)
Retrieves an entity by its id.
|
protected PaginatedListTo<E> |
findPaginated(SearchCriteriaTo criteria,
com.mysema.query.jpa.impl.JPAQuery query,
com.mysema.query.types.Expression<E> expr)
Returns a paginated list of entities according to the supplied
criteria . |
protected PaginatedListTo<E> |
findPaginated(SearchCriteriaTo criteria,
javax.persistence.Query query,
com.mysema.query.types.Expression<E> expr) |
void |
forceIncrementModificationCounter(E entity)
Enforces to increment the
modificationCounter e.g. |
protected abstract Class<E> |
getEntityClass() |
protected javax.persistence.EntityManager |
getEntityManager() |
protected String |
getEntityName() |
protected boolean |
isNew(E entity)
Determines if the given
PersistenceEntity is new . |
E |
save(E entity)
Saves a given entity.
|
void |
save(Iterable<? extends E> entities)
Saves all given entities.
|
void |
setEntityManager(javax.persistence.EntityManager entityManager) |
private static final org.slf4j.Logger LOG
private javax.persistence.EntityManager entityManager
protected abstract Class<E> getEntityClass()
Class
reflecting the managed entity.protected javax.persistence.EntityManager getEntityManager()
EntityManager
instance.public void setEntityManager(javax.persistence.EntityManager entityManager)
entityManager
- the EntityManager
to inject.protected String getEntityName()
public E save(E entity)
GenericDao
save
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
entity
- the entity
to saveprotected boolean isNew(E entity)
PersistenceEntity
is new
.entity
- is the PersistenceEntity
to check.true
if new
, false
otherwise (e.g.
detached
or managed
.public void save(Iterable<? extends E> entities)
GenericDao
save
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
entities
- the entities
to savepublic void forceIncrementModificationCounter(E entity)
GenericDao
modificationCounter
e.g. to enforce
that a parent object gets locked when its children are modified.Order
and its contained
OrderPosition
. By default the users can modify an Order
and each of its OrderPosition
s in
parallel without getting a locking conflict. This can be desired. However, it can also be a demand that an
Order
gets approved and the user doing that is responsible for the total price as the sum of the prices of
each OrderPosition
. Now if another user is adding or changing an OrderPostion
belonging to that
Order
in parallel the Order
will get approved but the approved total price will differ from what
the user has actually seen when he clicked on approve. To prevent this the use-case to modify an
OrderPosition
can use this method to trigger a locking on the associated Order
. The implication is
also that two users changing an OrderPosition
associated with the same Order
in parallel will get a
conflict.forceIncrementModificationCounter
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
entity
- that is getting checked.public E findOne(ID id)
GenericDao
findOne
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
id
- must not be null.public E find(ID id) throws net.sf.mmm.util.exception.api.ObjectNotFoundUserException
GenericDao
find
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
id
- must not be null.net.sf.mmm.util.exception.api.ObjectNotFoundUserException
- if the requested entity does not exists (use GenericDao.findOne(Object)
to
prevent).public boolean exists(ID id)
GenericDao
exists
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
id
- must not be null.protected List<E> findAll()
Iterable
to find ALL managed entities
from the persistent store. Not
exposed to API by default as this might not make sense for all kind of entities.public List<E> findAll(Iterable<ID> ids)
GenericDao
public void delete(ID id)
GenericDao
delete
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
id
- must not be null.public void delete(E entity)
GenericDao
delete
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
entity
- the entity
to deletepublic void delete(Iterable<? extends E> entities)
GenericDao
delete
in interface GenericDao<ID,E extends net.sf.mmm.util.entity.api.PersistenceEntity<ID>>
entities
- the entities
to deleteprotected PaginatedListTo<E> findPaginated(SearchCriteriaTo criteria, javax.persistence.Query query, com.mysema.query.types.Expression<E> expr)
protected PaginatedListTo<E> findPaginated(SearchCriteriaTo criteria, com.mysema.query.jpa.impl.JPAQuery query, com.mysema.query.types.Expression<E> expr)
criteria
.
Applies limit
and offset
values to the supplied according to the supplied
pagination
information inside criteria
.
If a total count
of available entities is requested, will also execute a second
query, without pagination parameters applied, to obtain said count.
Will install a query timeout if SearchCriteriaTo.getSearchTimeout()
is not null.
criteria
- contains information about the requested page.query
- is a query which is preconfigured with the desired conditions for the search.expr
- is used for the final mapping from the SQL result to the entities.protected PaginationResultTo createPaginationResult(PaginationTo pagination, com.mysema.query.jpa.impl.JPAQuery query)
pagination result
for the given pagination
and query
.
Needs to be called before pagination is applied to the query
.
pagination
- contains information about the requested page.query
- is a query preconfigured with the desired conditions for the search.protected Long calculateTotalBeforePagination(PaginationTo pagination, com.mysema.query.jpa.impl.JPAQuery query)
query
would return without pagination applied.
Needs to be called before pagination is applied to the query
.
pagination
- is the pagination information as requested by the client.query
- is the query
for which to calculate the total.PaginationTo.isTotal()
is false.protected void applyPagination(PaginationTo pagination, com.mysema.query.jpa.impl.JPAQuery query)
pagination criteria
to the given JPAQuery
.pagination
- is the pagination criteria
to apply.query
- is the JPAQuery
to apply to.protected void applyCriteria(net.sf.mmm.util.search.base.AbstractSearchCriteria criteria, com.mysema.query.jpa.impl.JPAQuery query)
search criteria
to the given JPAQuery
.criteria
- is the search criteria
to apply.query
- is the JPAQuery
to apply to.protected void applyCriteria(net.sf.mmm.util.search.base.AbstractSearchCriteria criteria, javax.persistence.Query query)
search criteria
to the given Query
.criteria
- is the search criteria
to apply.query
- is the Query
to apply to.protected void applyCriteria(SearchCriteriaTo criteria, com.mysema.query.jpa.impl.JPAQuery query)
search criteria
to the given Query
.criteria
- is the search criteria
to apply.query
- is the JPAQuery
to apply to.Copyright © 2014–2016 OASP-Team. All rights reserved.