net.sf.sidaof.dao.jpa
Class AbstractJpaDao<K,E>

java.lang.Object
  extended by net.sf.sidaof.dao.AbstractDao<K,E>
      extended by net.sf.sidaof.dao.jpa.AbstractJpaDao<K,E>
Type Parameters:
K - The primary key class of the entity, e.g. Long.
E - The entity class the DAO persists, e.g. Person.
All Implemented Interfaces:
Dao<K,E>, JpaDao<K,E>
Direct Known Subclasses:
JpaDaoImpl

public abstract class AbstractJpaDao<K,E>
extends AbstractDao<K,E>
implements JpaDao<K,E>

Base class for JPA DAOs. To use, extend it in DAO classes and implement JpaDao.getEntityManager(). JpaDao.getEntityManager() is externalized to allow subclasses to obtain the correct entity manager, particularly when using multiple persistence units, and in the necessary manner, e.g. specifying the unitName:

 @PersistenceContext(unitName = "persistenceUnitName")
 private EntityManager entityManager;
 

See Also:
JpaDaoImpl

Field Summary
private static org.slf4j.Logger LOG
           
 
Fields inherited from class net.sf.sidaof.dao.AbstractDao
entityClass, QUERY_SINGLERESULT_MULTFOUND_LOGMSG, QUERY_SINGLERESULT_NOTFOUND_LOGMSG
 
Constructor Summary
AbstractJpaDao()
          Make a new instance, automatically determining the entity class.
AbstractJpaDao(Class<E> entityClass)
          Make a new instance, specifying the entity class.
 
Method Summary
 void clear()
          Clear the in-memory persistable state, including unflushed changes.
 boolean contains(E entity)
          Check if the entity belongs to the current persistence context.
 Long countAll(String where, String pathExpression)
          Count all values represented by the specified where clause and pathExpression for the entity class this DAO persists.
 List<E> findAll()
          Find all entities.
 List<E> findAll(int startPosition, int maxResult)
          Find all entities starting in the resultset specified by startPosition and obtaining up to the number specified by maxResult.
 E findById(K id)
          Find an entity by its id, and return null if not found.
 E findById(K id, javax.persistence.LockModeType lockMode, boolean exceptionIfNotFound)
          Find an entity by its id, optionally throwing an exception if not found (this prevents NullPointerExceptions from occurring later), and optionally locking the found entity.
 void flush()
          Synchronize the underlying persistent store with persistable state held in memory.
protected  void handleQueryException(String msg, javax.persistence.PersistenceException e, javax.persistence.Query q, boolean throwException)
           
 E merge(E entity)
          Merge the entity.
 void persist(E entity)
          Save the entity.
 void refresh(E entity)
          Reload the entity.
 void remove(E entity)
          Delete the entity.
 E runQueryWithSingleResult(javax.persistence.Query q)
          Run the specified query, using getSingleResult(), expecting a single entity result and returning null if not found.
 E runQueryWithSingleResult(javax.persistence.Query q, javax.persistence.LockModeType lockMode, boolean exceptionIfNotFound)
          Run the specified query, using getSingleResult(), expecting a single entity result and returning null if not found.
 
Methods inherited from class net.sf.sidaof.dao.AbstractDao
countAll, countAll, getEntityClass, persist
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sf.sidaof.dao.jpa.JpaDao
getEntityManager, setEntityManager
 
Methods inherited from interface net.sf.sidaof.dao.Dao
countAll, countAll, persist
 

Field Detail

LOG

private static final org.slf4j.Logger LOG
Constructor Detail

AbstractJpaDao

public AbstractJpaDao()
Make a new instance, automatically determining the entity class.


AbstractJpaDao

public AbstractJpaDao(Class<E> entityClass)
Make a new instance, specifying the entity class.

Parameters:
entityClass - The entity class to manage.
Method Detail

findById

public E findById(K id)
Description copied from interface: Dao
Find an entity by its id, and return null if not found.

Specified by:
findById in interface Dao<K,E>
Parameters:
id - The id (primary key) of the entity to find.
Returns:
The entity for the specified id, null when entity not found.

findById

public E findById(K id,
                  javax.persistence.LockModeType lockMode,
                  boolean exceptionIfNotFound)
Description copied from interface: JpaDao
Find an entity by its id, optionally throwing an exception if not found (this prevents NullPointerExceptions from occurring later), and optionally locking the found entity.

Specified by:
findById in interface JpaDao<K,E>
Parameters:
id - The id (primary key) of the entity to find.
lockMode - Lock the entity in the persistence context for the specified lockMode, or null for no locking.
exceptionIfNotFound - true to throw an exception if entity not found.
Returns:
The entity for the specified id, null when entity not found or exception thrown based on the param.

findAll

public List<E> findAll()
Description copied from interface: Dao
Find all entities.

Specified by:
findAll in interface Dao<K,E>
Returns:
List of all entities.

findAll

public List<E> findAll(int startPosition,
                       int maxResult)
Description copied from interface: Dao
Find all entities starting in the resultset specified by startPosition and obtaining up to the number specified by maxResult. This method is useful for pagination of results.

Specified by:
findAll in interface Dao<K,E>
Parameters:
startPosition - The starting position in the result set to retrieve.
maxResult - The maximum number of rows to retrieve.
Returns:
List of a subset of all entities, starting with startPosition and containing up to maxResult entries.

persist

public void persist(E entity)
Description copied from interface: Dao
Save the entity.

Specified by:
persist in interface Dao<K,E>
Parameters:
entity - The persistence entity to save.

merge

public E merge(E entity)
Description copied from interface: Dao
Merge the entity.

Specified by:
merge in interface Dao<K,E>
Parameters:
entity - The persistence entity to merge.
Returns:
The merged entity.

remove

public void remove(E entity)
Description copied from interface: Dao
Delete the entity.

Specified by:
remove in interface Dao<K,E>
Parameters:
entity - The persistence entity to delete.

clear

public void clear()
Description copied from interface: Dao
Clear the in-memory persistable state, including unflushed changes.

Specified by:
clear in interface Dao<K,E>

flush

public void flush()
Description copied from interface: Dao
Synchronize the underlying persistent store with persistable state held in memory.

Specified by:
flush in interface Dao<K,E>

contains

public boolean contains(E entity)
Description copied from interface: Dao
Check if the entity belongs to the current persistence context.

Specified by:
contains in interface Dao<K,E>
Parameters:
entity - The persistence entity to see if exists.
Returns:
true if the entity belongs to the current persistence context.

refresh

public void refresh(E entity)
Description copied from interface: Dao
Reload the entity.

Specified by:
refresh in interface Dao<K,E>
Parameters:
entity - The persistence entity to reload from the persistence context.

countAll

public Long countAll(String where,
                     String pathExpression)
Description copied from interface: Dao
Count all values represented by the specified where clause and pathExpression for the entity class this DAO persists.

This convenience method is meant for simple count queries. For complex ones, create custom ones in the product DAOs.

Specified by:
countAll in interface Dao<K,E>
Parameters:
where - The where clause to append to the count query to narrow the counted values. Do not prefix with "where" as it is automatically added. The underlying query uses "e" as the identification variable, so the where will use "e", e.g. "e.name = 'Fred'". Specifying null disregards a where clause.
pathExpression - A String representing the object graph navigation to the element to count. The underlying query uses "e" as the identification variable, so the pathExpression will u "e", e.g. "e.address.zip", "e.name", "e.id". Specify either an identification variable (e.g. "e") or a path expression (e.g. "e.address.zip"). Specifying null disregards a pathExpression (defaults to "*").
Returns:
Long representing number of values in the group.

runQueryWithSingleResult

public E runQueryWithSingleResult(javax.persistence.Query q)
Description copied from interface: JpaDao
Run the specified query, using getSingleResult(), expecting a single entity result and returning null if not found.

Specified by:
runQueryWithSingleResult in interface JpaDao<K,E>
Parameters:
q - The query to run getSingleResult() on.
Returns:
The single entity found from the query execution or null if not found.

runQueryWithSingleResult

public E runQueryWithSingleResult(javax.persistence.Query q,
                                  javax.persistence.LockModeType lockMode,
                                  boolean exceptionIfNotFound)
Description copied from interface: JpaDao
Run the specified query, using getSingleResult(), expecting a single entity result and returning null if not found.

Specified by:
runQueryWithSingleResult in interface JpaDao<K,E>
Parameters:
q - The query to run getSingleResult() on.
lockMode - Lock the entity in the persistence context for the specified lockMode, or null for no locking.
exceptionIfNotFound - true to throw an exception if entity not found.
Returns:
The single entity found from the query execution.

handleQueryException

protected void handleQueryException(String msg,
                                    javax.persistence.PersistenceException e,
                                    javax.persistence.Query q,
                                    boolean throwException)


Copyright © 2009-2011. All Rights Reserved.