org.silverpeas.persistence.repository.jpa
Class SilverpeasJpaEntityManager<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>

java.lang.Object
  extended by org.silverpeas.persistence.repository.jpa.SilverpeasJpaEntityManager<ENTITY,ENTITY_IDENTIFIER_TYPE>
Type Parameters:
ENTITY - specify the class name of the entity which is handled by the repository manager.
ENTITY_IDENTIFIER_TYPE - the identifier class name used by ENTITY for its primary key definition.
All Implemented Interfaces:
EntityRepository<ENTITY,ENTITY_IDENTIFIER_TYPE>
Direct Known Subclasses:
PersistentResourceDateReminderRepository

public class SilverpeasJpaEntityManager<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
extends Object
implements EntityRepository<ENTITY,ENTITY_IDENTIFIER_TYPE>

A Silverpeas dedicated entity manager that wraps the JPA EntityManager and that provides convenient methods to perform the CRUD operations on entities.

All repositories that use only JPA for managing the persistence of their entities should extends this JPA manager. If the different parts of an entity are persisted into several data source beside a SQL-based one, then this repository should be used within a delegation of JPA related 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:
Yohann Chastagnier

Constructor Summary
SilverpeasJpaEntityManager()
           
 
Method Summary
protected  ENTITY_IDENTIFIER_TYPE convertToEntityIdentifier(String idAsString)
          Converts the given String id into the right entity identifier.
protected  Collection<ENTITY_IDENTIFIER_TYPE> convertToEntityIdentifiers(Collection<String> idsAsString)
          Converts the given String ids into the right entity identifiers.
protected  Collection<ENTITY_IDENTIFIER_TYPE> convertToEntityIdentifiers(String... idsAsString)
          Converts the given String ids into the right entity identifiers.
 void delete(ENTITY... entity)
          Deletes entities.
 void delete(List<ENTITY> entities)
          Deletes entities.
 long deleteById(Collection<String> ids)
          Deletes entities by their ids.
 long deleteById(String... ids)
          Deletes entities by their ids.
protected  long deleteFromJpqlQuery(String jpqlQuery, NamedParameters parameters)
          Deletes entities from a jpql query.
protected  long deleteFromNamedQuery(String namedQuery, NamedParameters parameters)
          Deletes entities from a named query.
 List<ENTITY> findByCriteria(QueryCriteria criteria)
          Lists entities from the specified criteria.
 List<ENTITY> findByNamedQuery(String namedQuery, NamedParameters parameters)
          Finds the entities by the specified named query (a JPQL instruction) and with the specified parameters.
 ENTITY findOneByNamedQuery(String namedQuery, NamedParameters parameters)
          Finds the first or the single 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.
 List<ENTITY> getAll()
          Gets all persisted entities.
 List<ENTITY> getById(Collection<String> ids)
          Gets persisted entities by their ids.
 List<ENTITY> getById(String... ids)
          Gets persisted entities by their ids.
 ENTITY getById(String id)
          Gets a persisted entity by its id.
protected  Class<ENTITY> getEntityClass()
          Gets the entity class managed by the repository.
protected  Class<ENTITY_IDENTIFIER_TYPE> getEntityIdentifierClass()
          Gets the identifier class of the entity managed by the repository.
protected  ENTITY getFromJpqlString(String jpqlQuery, NamedParameters parameters)
          Gets an entity from a jpql query string.
protected
<AN_ENTITY>
AN_ENTITY
getFromJpqlString(String jpqlQuery, NamedParameters parameters, Class<AN_ENTITY> returnEntityType)
          Gets an entity from a jpql query string.
protected  ENTITY getFromNamedQuery(String namedQuery, NamedParameters parameters)
          Gets an entity from a named query.
protected
<AN_ENTITY>
AN_ENTITY
getFromNamedQuery(String namedQuery, NamedParameters parameters, Class<AN_ENTITY> returnEntityType)
          Gets an entity from a named query.
protected  int getMaximumItemsInClause()
          Gets the maximum items in a in clause.
protected  List<ENTITY> listFromJpqlString(String jpqlQuery, NamedParameters parameters)
          Lists entities from a jpql query string.
protected
<AN_ENTITY>
List<AN_ENTITY>
listFromJpqlString(String jpqlQuery, NamedParameters parameters, Class<AN_ENTITY> returnEntityType)
          Lists entities from a JPQL query string.
protected  List<ENTITY> listFromNamedQuery(String namedQuery, NamedParameters parameters)
          Lists entities from a named query.
protected
<AN_ENTITY>
List<AN_ENTITY>
listFromNamedQuery(String namedQuery, NamedParameters parameters, Class<AN_ENTITY> returnEntityType)
          Lists entities from a named query.
 NamedParameters newNamedParameters()
          Gets a new query parameter container.
 List<ENTITY> save(OperationContext context, ENTITY... entities)
          Persists entities : create (if id is null or empty) or update.
 ENTITY save(OperationContext context, ENTITY entity)
          Persists entity : create (if id is null or empty) or update.
 List<ENTITY> save(OperationContext context, List<ENTITY> entities)
          Persists entities : create (if id is null or empty) or update.
protected  void setMaximumItemsInClause(int maximumItemsInClause)
          Sets the maximum items in a in clause.
protected
<E> Collection<Collection<E>>
split(Collection<E> collection)
          Collection spliter, useful for in clauses.
protected  long updateFromJpqlQuery(String jpqlQuery, NamedParameters parameters)
          Updates entities from a jpql query.
protected  long updateFromNamedQuery(String namedQuery, NamedParameters parameters)
          Updates entities from a named query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SilverpeasJpaEntityManager

public SilverpeasJpaEntityManager()
Method Detail

getEntityClass

protected Class<ENTITY> getEntityClass()
Gets the entity class managed by the repository.

Returns:

getEntityIdentifierClass

protected Class<ENTITY_IDENTIFIER_TYPE> getEntityIdentifierClass()
Gets the identifier class of the entity managed by the repository.

Returns:

convertToEntityIdentifier

protected ENTITY_IDENTIFIER_TYPE convertToEntityIdentifier(String idAsString)
Converts the given String id into the right entity identifier.

Parameters:
idAsString -
Returns:

convertToEntityIdentifiers

protected Collection<ENTITY_IDENTIFIER_TYPE> convertToEntityIdentifiers(String... idsAsString)
Converts the given String ids into the right entity identifiers.

Parameters:
idsAsString -
Returns:

convertToEntityIdentifiers

protected Collection<ENTITY_IDENTIFIER_TYPE> convertToEntityIdentifiers(Collection<String> idsAsString)
Converts the given String ids into the right entity identifiers.

Parameters:
idsAsString -
Returns:

flush

public void flush()
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.


findByNamedQuery

public List<ENTITY> 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.

findOneByNamedQuery

public ENTITY findOneByNamedQuery(String namedQuery,
                                  NamedParameters parameters)
Finds the first or the single 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 or the single one that matches the specified query.

getAll

public List<ENTITY> getAll()
Description copied from interface: EntityRepository
Gets all persisted entities. (It is recommended to not use this method on huge persitent containers)

Specified by:
getAll in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Returns:

getById

public ENTITY getById(String id)
Description copied from interface: EntityRepository
Gets a persisted entity by its id.

Specified by:
getById in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Returns:

getById

public List<ENTITY> getById(String... ids)
Description copied from interface: EntityRepository
Gets persisted entities by their ids.

Specified by:
getById in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Returns:

getById

public List<ENTITY> getById(Collection<String> ids)
Description copied from interface: EntityRepository
Gets persisted entities by their ids.

Specified by:
getById in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Returns:

save

public ENTITY save(OperationContext context,
                   ENTITY entity)
Description copied from interface: EntityRepository
Persists entity : create (if id is null or empty) or update.

Specified by:
save in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Parameters:
context - the context of the save operation (containing the user saver for example).
entity - the entity to save.
Returns:
the created or updated entity.

save

public List<ENTITY> save(OperationContext context,
                         ENTITY... entities)
Description copied from interface: EntityRepository
Persists entities : create (if id is null or empty) or update.

Specified by:
save in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Parameters:
context - the context of the save operation (containing the user saver for example).
entities - the entities to save.
Returns:

save

public List<ENTITY> save(OperationContext context,
                         List<ENTITY> entities)
Description copied from interface: EntityRepository
Persists entities : create (if id is null or empty) or update.

Specified by:
save in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Parameters:
context - the context of the save operation (containing the user saver for example).
entities - the entities to save.
Returns:

delete

public void delete(ENTITY... entity)
Description copied from interface: EntityRepository
Deletes entities.

Specified by:
delete in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>

delete

public void delete(List<ENTITY> entities)
Description copied from interface: EntityRepository
Deletes entities.

Specified by:
delete in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>

deleteById

public long deleteById(String... ids)
Description copied from interface: EntityRepository
Deletes entities by their ids.

Specified by:
deleteById in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Returns:
number of deleted entities.

deleteById

public long deleteById(Collection<String> ids)
Description copied from interface: EntityRepository
Deletes entities by their ids.

Specified by:
deleteById in interface EntityRepository<ENTITY extends Entity<ENTITY,ENTITY_IDENTIFIER_TYPE>,ENTITY_IDENTIFIER_TYPE extends EntityIdentifier>
Returns:
number of deleted entities.

newNamedParameters

public NamedParameters newNamedParameters()
Gets a new query parameter container.

Returns:

getFromJpqlString

protected ENTITY getFromJpqlString(String jpqlQuery,
                                   NamedParameters parameters)
Gets an entity from a jpql query string.

Parameters:
jpqlQuery - the JPQL query in string.
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 <AN_ENTITY> AN_ENTITY getFromJpqlString(String jpqlQuery,
                                                  NamedParameters parameters,
                                                  Class<AN_ENTITY> returnEntityType)
Gets an entity from a jpql query string. If the result

Type Parameters:
AN_ENTITY - the type of the returned entities.
Parameters:
jpqlQuery - the JPQL query in string.
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 List<ENTITY> listFromJpqlString(String jpqlQuery,
                                          NamedParameters parameters)
Lists entities from a jpql query string.

Parameters:
jpqlQuery - the JPQL query in string.
parameters - the parameters to apply to the query.
Returns:
a list of entities matching the query and the parameters

listFromJpqlString

protected <AN_ENTITY> List<AN_ENTITY> listFromJpqlString(String jpqlQuery,
                                                         NamedParameters parameters,
                                                         Class<AN_ENTITY> returnEntityType)
Lists entities from a JPQL query string.

Type Parameters:
AN_ENTITY - the type of the returned entities.
Parameters:
jpqlQuery - the JPQL query in string.
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

findByCriteria

public List<ENTITY> findByCriteria(QueryCriteria criteria)
Lists entities from the specified criteria.

Parameters:
criteria - the criteria constraining the query and for which the entities to list have to satisfy.
Returns:
a list of entities matching specified criteria. If a pagination criterion is defined in the criteria, then the returned list is a PaginationList instance.

updateFromJpqlQuery

protected long updateFromJpqlQuery(String jpqlQuery,
                                   NamedParameters parameters)
Updates entities from a jpql query. Please using this method as little as possible. If it is used, please handle the technical following data : - last updated by (id of the user in the most of cases) - last update date (new Timestamp((new Date()).getTime())) - version (entity.version = (entity.version + 1)) Not to handle the above technical data could bring to a unsustainable entity state ...

Parameters:
jpqlQuery - the query string
parameters - the parameters to apply to the query.
Returns:
the number of updated entities.

deleteFromJpqlQuery

protected long deleteFromJpqlQuery(String jpqlQuery,
                                   NamedParameters parameters)
Deletes entities from a jpql query.

Parameters:
jpqlQuery - the query string.
parameters - the parameters to apply to the query.
Returns:
the number of deleted entities.

getFromNamedQuery

protected ENTITY getFromNamedQuery(String namedQuery,
                                   NamedParameters parameters)
Gets an entity from a named query.

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 <AN_ENTITY> AN_ENTITY getFromNamedQuery(String namedQuery,
                                                  NamedParameters parameters,
                                                  Class<AN_ENTITY> returnEntityType)
Gets an entity from a named query.

Type Parameters:
AN_ENTITY - 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 List<ENTITY> listFromNamedQuery(String namedQuery,
                                          NamedParameters parameters)
Lists entities from a named query.

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 <AN_ENTITY> List<AN_ENTITY> listFromNamedQuery(String namedQuery,
                                                         NamedParameters parameters,
                                                         Class<AN_ENTITY> returnEntityType)
Lists entities from a named query.

Type Parameters:
AN_ENTITY -
Parameters:
namedQuery -
parameters -
returnEntityType -
Returns:

updateFromNamedQuery

protected long updateFromNamedQuery(String namedQuery,
                                    NamedParameters parameters)
Updates entities from a named query. Please using this method as little as possible. If it is used, please handle the technical following data : - last updated by (id of the user in the most of cases) - last update date (new Timestamp((new Date()).getTime())) - version (entity.version = (entity.version + 1)) Not to handle the above technical data could bring to a unsustainable entity state ...

Parameters:
namedQuery -
parameters -
Returns:

deleteFromNamedQuery

protected long deleteFromNamedQuery(String namedQuery,
                                    NamedParameters parameters)
Deletes entities from a named query.

Parameters:
namedQuery -
parameters -
Returns:

split

protected <E> Collection<Collection<E>> split(Collection<E> collection)
Collection spliter, useful for in clauses.

Type Parameters:
E -
Parameters:
collection -
Returns:

getMaximumItemsInClause

protected int getMaximumItemsInClause()
Gets the maximum items in a in clause.

Returns:

setMaximumItemsInClause

protected void setMaximumItemsInClause(int maximumItemsInClause)
Sets the maximum items in a in clause.

Parameters:
maximumItemsInClause -


Copyright © 2016 Silverpeas. All Rights Reserved.