Class AbstractJpaEntityRepository<T extends AbstractJpaEntity<T,?>>
- java.lang.Object
-
- org.silverpeas.core.persistence.datasource.repository.jpa.AbstractJpaEntityRepository<T>
-
- Type Parameters:
T
- the class name of the identifiable entity which is handled by the repository.
- All Implemented Interfaces:
EntityRepository<T>
- Direct Known Subclasses:
BasicJpaEntityRepository
,SilverpeasJpaEntityRepository
public abstract class AbstractJpaEntityRepository<T extends AbstractJpaEntity<T,?>> extends Object implements EntityRepository<T>
Abstract implementation of theEntityRepository
interface that uses the JPA API. This implementation defines all the common methods that can be required by the more concrete repositories to implement easily their persistence related business operations. It provides additional signatures to handle friendly the JPA queries into extensions of repository classes. Take a look into this class to analyse how query parameters are performed (NamedParameters
).- Author:
- mmoquillon
-
-
Constructor Summary
Constructors Constructor Description AbstractJpaEntityRepository()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(T entity)
Does this repository contains the specified entity?protected long
countByCriteria(QueryCriteria criteria)
protected long
countFromJpqlString(String query, NamedParameters parameters)
Gets an entity from the specified query written in JPQL and with the specified parameters.void
delete(List<T> entities)
Deletes entities.long
deleteById(Collection<String> ids)
Deletes entities by their ids.protected long
deleteFromJpqlQuery(String query, NamedParameters parameters)
Deletes entities from a JPQL query and with the specified parameters.protected long
deleteFromNamedQuery(String namedQuery, NamedParameters parameters)
Deletes the entities from a named query and with the specified parameters.SilverpeasList<T>
findByCriteria(QueryCriteria criteria)
Finds all the entities that match the specified criteria.protected SilverpeasList<T>
findByNamedQuery(String namedQuery, NamedParameters parameters)
Finds the entities by the specified named query (a JPQL instruction) and with the specified parameters.protected T
findFirstByNamedQuery(String namedQuery, NamedParameters parameters)
Finds the first entity matching the specified named query (a JPQL instruction) and with the specified parameters.void
flush()
Synchronizes the persistence context to the underlying data source.SilverpeasList<T>
getAll()
Gets all the persisted entities.T
getById(String id)
Gets a persisted entity by its unique identifier.SilverpeasList<T>
getById(Collection<String> ids)
Gets persisted entities by their ids.protected Class<T>
getEntityClass()
protected javax.persistence.EntityManager
getEntityManager()
Gets the entity manager with which the persistence of the entities stored in this repository is performed.protected T
getFromJpqlString(String query, NamedParameters parameters)
Gets an entity from the specified query written in JPQL and with the specified parameters.protected <U> U
getFromJpqlString(String query, NamedParameters parameters, Class<U> returnEntityType)
Gets an entity of a given type from the specified JPQL query and with the specified parameters.protected T
getFromNamedQuery(String namedQuery, NamedParameters parameters)
Gets an entity from a named query and with the specified parameters.protected <U> U
getFromNamedQuery(String namedQuery, NamedParameters parameters, Class<U> returnEntityType)
Gets an entity from a named query and with the specified parameters.protected EntityIdentifierConverter
getIdentifierConverter()
protected int
getMaximumItemsInClause()
Maximum number of items to be passed into a SQL "in" clause.protected SilverpeasList<T>
listFromJpqlString(String query, NamedParameters parameters)
Lists entities from the specified JPQL query and with the specified parameters.protected <U> SilverpeasList<U>
listFromJpqlString(String query, NamedParameters parameters, Class<U> returnEntityType)
Lists entities from the specified JPQL query and with the specified parameters.protected SilverpeasList<T>
listFromJpqlString(String query, NamedParameters parameters, PaginationCriterion pagination)
Lists entities from the specified JPQL query and with the specified parameters.protected <U> SilverpeasList<U>
listFromJpqlString(String jpqlQuery, NamedParameters parameters, PaginationCriterion pagination, Class<U> returnEntityType)
Lists entities from the specified JPQL query and with the specified parameters.protected SilverpeasList<T>
listFromNamedQuery(String namedQuery, NamedParameters parameters)
Lists entities from a named query and with the specified parameters.protected <U> SilverpeasList<U>
listFromNamedQuery(String namedQuery, NamedParameters parameters, Class<U> returnEntityType)
Lists entities from a named query and with the specified parameters.protected NamedParameters
newNamedParameters()
Constructs a container of query parameters to use in JPQL queries.protected NoNamedParameter
noParameter()
Gets an instance that represents the fact that it does not exist parameter for the query to execute.SilverpeasList<T>
save(List<T> entities)
Persists entities : create (if id is null or empty) or update.protected void
setMaximumItemsInClause(int maximumItemsInClause)
Sets the maximum items in clause.protected <E> Collection<Collection<E>>
split(Collection<E> collection)
protected Stream<T>
streamByNamedQuery(String namedQuery, NamedParameters parameters)
Streams entities from a named query and with the specified parameters.
Useful for treatment over a large number of data.
Please be careful to always close the stream in order to avoid memory leaks!!!protected <U> Stream<U>
streamByNamedQuery(String namedQuery, NamedParameters parameters, Class<U> returnEntityType)
Streams entities from a named query and with the specified parameters.
Useful for treatment over a large number of data.
Please be careful to always close the stream in order to avoid memory leaks!!!protected Stream<T>
streamFromJpqlString(String query, NamedParameters parameters)
Streams entities from the specified JPQL query and with the specified parameters.
Useful for treatment over a large number of data.protected <U> Stream<U>
streamFromJpqlString(String query, NamedParameters parameters, Class<U> returnEntityType)
Stream entities from the specified JPQL query and with the specified parameters.protected long
updateFromJpqlQuery(String query, NamedParameters parameters)
Updates entities from a JPQL query and with the specified parameters.protected long
updateFromNamedQuery(String namedQuery, NamedParameters parameters)
Updates the entities from a named query and with the specified parameters.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.silverpeas.core.persistence.datasource.repository.EntityRepository
delete, deleteByComponentInstanceId, deleteById, getById, save, save
-
-
-
-
Method Detail
-
save
public SilverpeasList<T> save(List<T> entities)
Description copied from interface:EntityRepository
Persists entities : create (if id is null or empty) or update.- Specified by:
save
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
- Parameters:
entities
- the entities to save.- Returns:
- the created or updated entity.
-
flush
public void flush()
Description copied from interface:EntityRepository
Synchronizes the persistence context to the underlying data source. Within a transactional context, the persistence context is directly put to the data source but will be effective only when the transaction will be committed. The consequence of the synchronization within a transaction context is the persistence context is then validated by the data source. Making it work, the data source has to support the transactions.Warning, the behavior of this method is implementation-dependent. According to the type of the repository or of the underlying data source, the flush can not to be working.
- Specified by:
flush
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
-
contains
public boolean contains(T entity)
Description copied from interface:EntityRepository
Does this repository contains the specified entity? It contains the entity if its persistence context is taken in charge by the instances of the repository class.- Specified by:
contains
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
- Parameters:
entity
- an entity.- Returns:
- true if the specified entity exists in the persistence context backed by this repository, false otherwise.
-
getAll
public SilverpeasList<T> getAll()
Description copied from interface:EntityRepository
Gets all the persisted entities. WARNING: we don't recommend to use this method with a lot of persisted entities.- Specified by:
getAll
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
- Returns:
- a list of all the persisted entities.
-
getById
public T getById(String id)
Description copied from interface:EntityRepository
Gets a persisted entity by its unique identifier.- Specified by:
getById
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
- Parameters:
id
- a unique identifier.- Returns:
- the entity with the specified entity identifier or null if no such entity exist in the data source.
-
getById
public SilverpeasList<T> getById(Collection<String> ids)
Description copied from interface:EntityRepository
Gets persisted entities by their ids.- Specified by:
getById
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
- Parameters:
ids
- the unique identifiers of the entity to get.- Returns:
- a list of entities.
-
delete
public void delete(List<T> entities)
Description copied from interface:EntityRepository
Deletes entities.- Specified by:
delete
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
- Parameters:
entities
- the entities to delete.
-
deleteById
public long deleteById(Collection<String> ids)
Description copied from interface:EntityRepository
Deletes entities by their ids.- Specified by:
deleteById
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
- Parameters:
ids
- the identifiers of the entities to delete.- Returns:
- number of deleted entities.
-
countByCriteria
protected long countByCriteria(QueryCriteria criteria)
-
findByCriteria
public SilverpeasList<T> findByCriteria(QueryCriteria criteria)
Description copied from interface:EntityRepository
Finds all the entities that match the specified criteria.- Specified by:
findByCriteria
in interfaceEntityRepository<T extends AbstractJpaEntity<T,?>>
- Parameters:
criteria
- the criteria constraining the query and for which the entities to list have to satisfy.- Returns:
- a list of entities matching the specified criteria. If a pagination criterion is
defined in the criteria, then the returned list is a
PaginationList
instance.
-
findByNamedQuery
protected SilverpeasList<T> findByNamedQuery(String namedQuery, NamedParameters parameters)
Finds the entities by the specified named query (a JPQL instruction) and with the specified parameters.- Parameters:
namedQuery
- the named query. It is an identifier to a JPQL instruction.parameters
- the parameters to apply on the query.- Returns:
- a list of entities that match the specified query.
-
findFirstByNamedQuery
protected T findFirstByNamedQuery(String namedQuery, NamedParameters parameters)
Finds the first entity matching the specified named query (a JPQL instruction) and with the specified parameters.- Parameters:
namedQuery
- the named query. It is an identifier to a JPQL instruction.parameters
- the parameters to apply on the query.- Returns:
- the first encountered entity that matches the specified query or null if no entities match the specified query.
-
newNamedParameters
protected NamedParameters newNamedParameters()
Constructs a container of query parameters to use in JPQL queries.- Returns:
- a container of query parameters.
-
noParameter
protected NoNamedParameter noParameter()
Gets an instance that represents the fact that it does not exist parameter for the query to execute.- Returns:
- instance of
NoNamedParameter
-
countFromJpqlString
protected long countFromJpqlString(String query, NamedParameters parameters)
Gets an entity from the specified query written in JPQL and with the specified parameters.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.- Returns:
- the required entity if exists, null otherwise
- Throws:
IllegalArgumentException
- if it exists more than one entity from the query result.
-
getFromJpqlString
protected T getFromJpqlString(String query, NamedParameters parameters)
Gets an entity from the specified query written in JPQL and with the specified parameters.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.- Returns:
- the required entity if exists, null otherwise
- Throws:
IllegalArgumentException
- if it exists more than one entity from the query result.
-
getFromJpqlString
protected <U> U getFromJpqlString(String query, NamedParameters parameters, Class<U> returnEntityType)
Gets an entity of a given type from the specified JPQL query and with the specified parameters. This method is for fetching any information about the entities stored into this repository; it can be an entity itself or a count of entities satisfying some properties, and so on.- Type Parameters:
U
- the type of the returned entities.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.returnEntityType
- the class of the returned entities.- Returns:
- the required entity if exists, null otherwise
- Throws:
IllegalArgumentException
- if it exists more than one entity from the query result.
-
listFromJpqlString
protected SilverpeasList<T> listFromJpqlString(String query, NamedParameters parameters)
Lists entities from the specified JPQL query and with the specified parameters.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.- Returns:
- a list of entities matching the query and the parameters. If no entities match the query then an empty list is returned.
-
listFromJpqlString
protected SilverpeasList<T> listFromJpqlString(String query, NamedParameters parameters, PaginationCriterion pagination)
Lists entities from the specified JPQL query and with the specified parameters.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.pagination
- the pagination criterion to apply.- Returns:
- a list of entities matching the query and the parameters. If no entities match the query then an empty list is returned.
-
listFromJpqlString
protected <U> SilverpeasList<U> listFromJpqlString(String query, NamedParameters parameters, Class<U> returnEntityType)
Lists entities from the specified JPQL query and with the specified parameters. This method is for fetching any information about the entities stored into this repository; it can be the entities themselves or some of their properties or relationships, and so on.- Type Parameters:
U
- the type of the returned entities.
Please be careful to always close the streams in order to avoid memory leaks!!!try(Stream<T> object : streamAllFromQuery(...)) { // Performing the treatment }
- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.returnEntityType
- the class of the returned entities.- Returns:
- a list of entities matching the query and the parameters. If no entities match the query then an empty list is returned.
-
listFromJpqlString
protected <U> SilverpeasList<U> listFromJpqlString(String jpqlQuery, NamedParameters parameters, PaginationCriterion pagination, Class<U> returnEntityType)
Lists entities from the specified JPQL query and with the specified parameters. This method is for fetching any information about the entities stored into this repository; it can be the entities themselves or some of their properties or relationships, and so on.- Type Parameters:
U
- the type of the returned entities.- Parameters:
jpqlQuery
- the JPQL query.parameters
- the parameters to apply to the query.pagination
- the pagination criterion to apply.returnEntityType
- the class of the returned entities.- Returns:
- a list of entities matching the query and the parameters. If no entities match the query then an empty list is returned.
-
streamFromJpqlString
protected Stream<T> streamFromJpqlString(String query, NamedParameters parameters)
Streams entities from the specified JPQL query and with the specified parameters.
Useful for treatment over a large number of data.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.- Returns:
- a list of entities matching the query and the parameters. If no entities match the query then an empty list is returned.
-
streamFromJpqlString
protected <U> Stream<U> streamFromJpqlString(String query, NamedParameters parameters, Class<U> returnEntityType)
Stream entities from the specified JPQL query and with the specified parameters. This method is for fetching any information about the entities stored into this repository; it can be the entities themselves or some of their properties or relationships, and so on.
Useful for treatment over a large number of data.
Please be careful to always close the streams in order to avoid memory leaks!!!try(Stream<T> object : streamAllFromQuery(...)) { // Performing the treatment }
- Type Parameters:
U
- the type of the returned entities.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.returnEntityType
- the class of the returned entities.- Returns:
- a list of entities matching the query and the parameters. If no entities match the query then an empty list is returned.
-
updateFromJpqlQuery
protected long updateFromJpqlQuery(String query, NamedParameters parameters)
Updates entities from a JPQL query and with the specified parameters.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.- Returns:
- the number of deleted entities.
-
deleteFromJpqlQuery
protected long deleteFromJpqlQuery(String query, NamedParameters parameters)
Deletes entities from a JPQL query and with the specified parameters.- Parameters:
query
- the JPQL query.parameters
- the parameters to apply to the query.- Returns:
- the number of deleted entities.
-
getFromNamedQuery
protected T getFromNamedQuery(String namedQuery, NamedParameters parameters)
Gets an entity from a named query and with the specified parameters.- Parameters:
namedQuery
- the name of the query.parameters
- the parameters to apply to the query.- Returns:
- the required entity if exists, null otherwise
- Throws:
IllegalArgumentException
- if it exists more than one entity from the query result.
-
getFromNamedQuery
protected <U> U getFromNamedQuery(String namedQuery, NamedParameters parameters, Class<U> returnEntityType)
Gets an entity from a named query and with the specified parameters. This method is for fetching any information about the entities stored into this repository; it can be an entity itself or a count of entities satisfying some properties, and so on.- Type Parameters:
U
- the type of the returned entities.- Parameters:
namedQuery
- the name of the query.parameters
- the parameters to apply to the query.returnEntityType
- the class of the returned entities.- Returns:
- the required entity if exists, null otherwise
- Throws:
IllegalArgumentException
- if it exists more than one entity from the query result.
-
listFromNamedQuery
protected SilverpeasList<T> listFromNamedQuery(String namedQuery, NamedParameters parameters)
Lists entities from a named query and with the specified parameters.- Parameters:
namedQuery
- the n ame of the query.parameters
- the parameters to apply to the query.- Returns:
- the list of entities matching the query and the parameters.
-
listFromNamedQuery
protected <U> SilverpeasList<U> listFromNamedQuery(String namedQuery, NamedParameters parameters, Class<U> returnEntityType)
Lists entities from a named query and with the specified parameters.- Type Parameters:
U
- the type of the returned entities.- Parameters:
namedQuery
- the name of the query.parameters
- the parameters to apply to the query.returnEntityType
- the class of the returned entities.- Returns:
- a list of entities of the given type or an empty list if no entities match the specified named query with the given parameters.
-
streamByNamedQuery
protected Stream<T> streamByNamedQuery(String namedQuery, NamedParameters parameters)
Streams entities from a named query and with the specified parameters.
Useful for treatment over a large number of data.
Please be careful to always close the stream in order to avoid memory leaks!!!try(Stream<T> object : streamAllFromQuery(...)) { // Performing the treatment }
- Parameters:
namedQuery
- the n ame of the query.parameters
- the parameters to apply to the query.- Returns:
- the list of entities matching the query and the parameters.
-
streamByNamedQuery
protected <U> Stream<U> streamByNamedQuery(String namedQuery, NamedParameters parameters, Class<U> returnEntityType)
Streams entities from a named query and with the specified parameters.
Useful for treatment over a large number of data.
Please be careful to always close the stream in order to avoid memory leaks!!!try(Stream<T> object : streamAllFromQuery(...)) { // Performing the treatment }
- Type Parameters:
U
- the type of the returned entities.- Parameters:
namedQuery
- the name of the query.parameters
- the parameters to apply to the query.returnEntityType
- the class of the returned entities.- Returns:
- a list of entities of the given type or an empty list if no entities match the specified named query with the given parameters.
-
updateFromNamedQuery
protected long updateFromNamedQuery(String namedQuery, NamedParameters parameters)
Updates the entities from a named query and with the specified parameters.- Parameters:
namedQuery
- the name of the query.parameters
- the parameters to apply to the query.- Returns:
- the count of entities updated by the named query.
-
deleteFromNamedQuery
protected long deleteFromNamedQuery(String namedQuery, NamedParameters parameters)
Deletes the entities from a named query and with the specified parameters.- Parameters:
namedQuery
- the name of the query.parameters
- the parameters to apply to the query.- Returns:
- the count of entities deleted by the named query.
-
getMaximumItemsInClause
protected int getMaximumItemsInClause()
Maximum number of items to be passed into a SQL "in" clause. Indeed, according to the database, the treatment of the "in" clause may be different and pass too much value "in" this clause may result in an error ... The behaviour is the following : when a query includes an "in" clause potentially filled with a lot of values, the protected split method can be used to divide a collection of values into several collections of values, each one with its size limited to this parameter. Please take a look at.EntityRepository#getByIdentifier
or.repository .EntityRepository#deleteByIdentifier
methods for examples of use. Returns of experiences drives us to set the default value at 500.- Returns:
- the maximum number of items in a clause.
-
setMaximumItemsInClause
protected void setMaximumItemsInClause(int maximumItemsInClause)
Sets the maximum items in clause.- Parameters:
maximumItemsInClause
- the maximum number of items in an SQL in-clause.
-
getEntityManager
protected javax.persistence.EntityManager getEntityManager()
Gets the entity manager with which the persistence of the entities stored in this repository is performed.- Returns:
- the JPA entity manager.
-
getIdentifierConverter
protected EntityIdentifierConverter getIdentifierConverter()
-
split
protected <E> Collection<Collection<E>> split(Collection<E> collection)
-
-