org.openwms.core.integration.jpa
Class AbstractGenericJpaDao<T extends AbstractEntity,ID extends Serializable>

java.lang.Object
  extended by org.openwms.core.integration.jpa.AbstractGenericJpaDao<T,ID>
Type Parameters:
T - Any serializable type, mostly an Entity class type.
ID - The type of the Entity class' unique id
All Implemented Interfaces:
GenericDao<T,ID>
Direct Known Subclasses:
GenericJpaDaoImpl, I18nRepositoryImpl, LocationDaoImpl, LocationGroupDaoImpl, LocationTypeDaoImpl, ModuleDaoImpl, RoleDaoImpl, TransportOrderDaoImpl, TransportUnitDaoImpl, TransportUnitTypeDaoImpl, UserDaoImpl

@Transactional(propagation=MANDATORY)
public abstract class AbstractGenericJpaDao<T extends AbstractEntity,ID extends Serializable>
extends Object
implements GenericDao<T,ID>

An AbstractGenericJpaDao - Extend this DAO implementation to inherit simple JPA CRUD operations.

This GenericDao implementation acts as a transactional superclass to have a benefit from Springs exception translation and transaction management.

Furthermore an AbstractGenericJpaDao has transactional behavior expressed with Springs Transactional annotation.

Since:
0.1
Version:
$Revision: 1541 $
Author:
Heiko Scherrer
See Also:
Repository, Transactional

Field Summary
protected  org.slf4j.Logger logger
          Logger instance can be used by subclasses.
 
Fields inherited from interface org.openwms.core.integration.GenericDao
FIND_ALL, FIND_BY_ID
 
Constructor Summary
protected AbstractGenericJpaDao()
          Create a new AbstractGenericJpaDao.
 
Method Summary
protected  void beforeUpdate(T entity)
          This method is considered as a hook to do something before an update is performed.
 List<T> findAll()
          Find all entities and return them as a List.
 T findById(ID id)
          Find and return the entity identified by the technical key.
 List<T> findByNamedParameters(String queryName, Map<String,?> params)
          Use a named query to find all entities.
 List<T> findByPositionalParameters(String queryName, Object... values)
          Use an own JPA query to fetch entities.
 T findByUniqueId(Serializable id)
          Find and return the entity identified by the natural unique id.
protected  javax.persistence.EntityManager getEm()
          Subclasses can call this method to retrieve an shared EntityManager instance.
protected abstract  String getFindAllQuery()
          Returns the name of the NamedQuery to find all Entity classes.
protected abstract  String getFindByUniqueIdQuery()
          Returns the name of the NamedQuery to find an Entity by the business key.
protected  Class<T> getPersistentClass()
          Returns the entity class to deals with.
The Java Reflection API is used to find the type.
 void persist(T entity)
          Persist a transient entity.
 void remove(T entity)
          Removes an already persistent entity.
 T save(T entity)
          Synchronize an entity with the persistence layer and return it.
 void setPersistentClass(Class<T> persistentClass)
          Set the type of entity to deal with.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.slf4j.Logger logger
Logger instance can be used by subclasses.

Constructor Detail

AbstractGenericJpaDao

protected AbstractGenericJpaDao()
Create a new AbstractGenericJpaDao.

Method Detail

getPersistentClass

protected Class<T> getPersistentClass()
Returns the entity class to deals with.
The Java Reflection API is used to find the type.

Returns:
Entity class type.

setPersistentClass

public void setPersistentClass(Class<T> persistentClass)
Set the type of entity to deal with.

Specified by:
setPersistentClass in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Parameters:
persistentClass - Class type of the entity instance

findById

public T findById(ID id)
Find and return the entity identified by the technical key.

Specified by:
findById in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Parameters:
id - Unique technical key to find the entity
Returns:
Entity class identified by id

findAll

public List<T> findAll()
Find all entities and return them as a List.

Specified by:
findAll in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Returns:
List of all entities

findByNamedParameters

public List<T> findByNamedParameters(String queryName,
                                     Map<String,?> params)
Use a named query to find all entities. Pass in the name of the NamedQuery and a parameter map.

Specified by:
findByNamedParameters in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Parameters:
queryName - Defined name of the NamedQuery
params - Map of parameters to pass to the query
Returns:
List of found entity classes

findByPositionalParameters

public List<T> findByPositionalParameters(String queryName,
                                          Object... values)
Use an own JPA query to fetch entities.

Specified by:
findByPositionalParameters in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Parameters:
queryName - Defined name of the NamedQuery
values - A list of values to use as parameters
Returns:
A list of all entities

findByUniqueId

public T findByUniqueId(Serializable id)
Find and return the entity identified by the natural unique id. A NoUniqueResultException is thrown when more than one entity was found. In case of no result, null is returned.

Specified by:
findByUniqueId in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Parameters:
id - Natural key to find the entity (also known as business key)
Returns:
The entity or null
Throws:
NoUniqueResultException - when more than one entity was found

save

public T save(T entity)
Synchronize an entity with the persistence layer and return it.

Specified by:
save in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Parameters:
entity - Entity instance to be synchronized with the persistence layer
Returns:
The synchronized entity instance. If JPA is used as implementation, the returned instance is managed.

remove

public void remove(T entity)
Removes an already persistent entity.

Specified by:
remove in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Parameters:
entity - Entity instance to be removed

persist

public void persist(T entity)
Persist a transient entity.

Specified by:
persist in interface GenericDao<T extends AbstractEntity,ID extends Serializable>
Parameters:
entity - Entity instance to be persisted

getFindAllQuery

protected abstract String getFindAllQuery()
Returns the name of the NamedQuery to find all Entity classes.

Returns:
Name of the query

getFindByUniqueIdQuery

protected abstract String getFindByUniqueIdQuery()
Returns the name of the NamedQuery to find an Entity by the business key.

Returns:
Name of the query

beforeUpdate

protected void beforeUpdate(T entity)
This method is considered as a hook to do something before an update is performed.

Parameters:
entity - The Entity that is updated

getEm

protected final javax.persistence.EntityManager getEm()
Subclasses can call this method to retrieve an shared EntityManager instance.

Returns:
The EntityManager


Copyright © 2005-2011 openwms.org. All Rights Reserved.