org.perfectjpattern.jee.integration.dao
Class JpaBaseDao<Id extends Serializable,Element>

java.lang.Object
  extended by org.perfectjpattern.jee.integration.dao.JpaBaseDao<Id,Element>
All Implemented Interfaces:
IBaseDao<Id,Element>, IBaseReadOnlyDao<Id,Element>
Direct Known Subclasses:
AbstractJpaManagedBaseDao, HibernateGenericDao

public class JpaBaseDao<Id extends Serializable,Element>
extends Object
implements IBaseDao<Id,Element>

Abstract reusable JPA partial implementation of IGenericReadOnlyDao that provides automatic handling of EntityManager within a EJB container

Version:
$Revision: 1.0 $Date: Feb 11, 2009 10:17:46 AM $
Author:
Giovanni Azua
 

Constructor Summary
protected JpaBaseDao(Class<Element> aPersistentClass, ISessionStrategy aSessionStrategy, ITransactionStrategy aTransactionStrategy)
          Constructs a HibernateGenericReadOnlyDao instance from the persistent class type, the ISessionStrategy that creates ISession instances and the ITransactionStrategy that creates ITransaction instances
 
Method Summary
 boolean contains(Element anElement)
          Returns true if the specified Element exists in the persistent storage, false otherwise.
 int count()
          Returns the total number of records.
 Id create(Element anElement)
          Returns the ID of the newly created Element.
 boolean delete(Element anElement)
          Returns true if the Element was deleted, false otherwise i.e.
 void deleteAll()
          Deletes all Element from the persistent storage.
 List<Element> findAll()
          Returns all Elements from the persistent storage.
 Element findById(Id anId)
          Returns the Element matching the given ID.
protected  Class<Element> getPersistentClass()
          Returns the persistentClass
 ISession getSession()
          Returns the ISession adapted from the implementation-specific
protected  ISessionStrategy getSessionStrategy()
          Returns the sessionStrategy
 ITransaction getTransaction()
          Returns the ITransaction adapted from the implementation-specific
protected  ITransactionStrategy getTransactionStrategy()
          Returns the transactionStrategy
protected  void setEntityManager(javax.persistence.EntityManager anEntityManager)
          Sets the EntityManager, called from a managed EJB that exposes IGenericReadOnlyDao or IGenericDao remotely.
 boolean update(Element anElement)
          Returns true if the Element was updated, false otherwise i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpaBaseDao

protected JpaBaseDao(Class<Element> aPersistentClass,
                     ISessionStrategy aSessionStrategy,
                     ITransactionStrategy aTransactionStrategy)
              throws IllegalArgumentException
Constructs a HibernateGenericReadOnlyDao instance from the persistent class type, the ISessionStrategy that creates ISession instances and the ITransactionStrategy that creates ITransaction instances

Parameters:
aPersistentClass - The persistent Java Bean class
aSessionStrategy - Factory that creates Sessions
aTransactionStrategy - Factory that creates Transaction
Throws:
IllegalArgumentException
Method Detail

contains

public boolean contains(Element anElement)
                 throws DaoException,
                        IllegalArgumentException
Returns true if the specified Element exists in the persistent storage, false otherwise.

Specified by:
contains in interface IBaseReadOnlyDao<Id extends Serializable,Element>
Parameters:
anElement - Element to find in this persistent storage.
Returns:
true if Element exists in the persistent storage, false otherwise
Throws:
DaoException
IllegalArgumentException - 'anId' must not be null.

count

public int count()
          throws DaoException
Returns the total number of records. Provides a high-level reusable means to do a "SELECT * FROM ...". Noteworthy that every call to this function will trigger a new execution of a statement. Therefore if the count value is needed several times in the client code it is a good idea to cache it there.

Specified by:
count in interface IBaseReadOnlyDao<Id extends Serializable,Element>
Returns:
total number of records
Throws:
DaoException

findById

public Element findById(Id anId)
                 throws DaoException,
                        IllegalArgumentException
Returns the Element matching the given ID. Retrieves an Element from the persistent storage using as input its ID.

Specified by:
findById in interface IBaseReadOnlyDao<Id extends Serializable,Element>
Parameters:
anId - ID of the Element to retrieve.
Returns:
Element matching the given ID
Throws:
DaoException
IllegalArgumentException - 'anId' must not be null.

findAll

public List<Element> findAll()
                      throws DaoException
Returns all Elements from the persistent storage.

Specified by:
findAll in interface IBaseReadOnlyDao<Id extends Serializable,Element>
Returns:
all Elements from the persistent storage.
Throws:
DaoException

create

public Id create(Element anElement)
                               throws DaoException,
                                      IllegalArgumentException
Returns the ID of the newly created Element. Creates an Element in the persistent storage and returns its ID.

Specified by:
create in interface IBaseDao<Id extends Serializable,Element>
Parameters:
anElement - Element to create
Returns:
ID of the newly created Element
Throws:
DaoException
IllegalArgumentException - 'anElement' must not be null.

update

public boolean update(Element anElement)
               throws DaoException
Returns true if the Element was updated, false otherwise i.e. the Element was not found. Updates the given Element in the persistent storage.

Specified by:
update in interface IBaseDao<Id extends Serializable,Element>
Parameters:
anElement - Element to update
Returns:
true if the Element was updated, false otherwise.
Throws:
DaoException

delete

public boolean delete(Element anElement)
               throws DaoException
Returns true if the Element was deleted, false otherwise i.e. the Element was not found. Deletes the given Element from the persistent storage.

Specified by:
delete in interface IBaseDao<Id extends Serializable,Element>
Parameters:
anElement - Element to delete
Returns:
true if the Element was deleted, false otherwise.
Throws:
DaoException

deleteAll

public void deleteAll()
               throws DaoException
Deletes all Element from the persistent storage.

Specified by:
deleteAll in interface IBaseDao<Id extends Serializable,Element>
Throws:
DaoException

getTransaction

public ITransaction getTransaction()
Returns the ITransaction adapted from the implementation-specific

Specified by:
getTransaction in interface IBaseReadOnlyDao<Id extends Serializable,Element>
Returns:
ITransaction adapted from the implementation-specific
See Also:
IAdapter

getSession

public ISession getSession()
Returns the ISession adapted from the implementation-specific

Specified by:
getSession in interface IBaseReadOnlyDao<Id extends Serializable,Element>
Returns:
ISession adapted from the implementation-specific
See Also:
IAdapter

setEntityManager

protected void setEntityManager(javax.persistence.EntityManager anEntityManager)
                         throws IllegalArgumentException
Sets the EntityManager, called from a managed EJB that exposes IGenericReadOnlyDao or IGenericDao remotely.

Parameters:
anEntityManager - The EntityManager to set
Throws:
IllegalArgumentException - 'anEntityManager' must not be null

getPersistentClass

protected final Class<Element> getPersistentClass()
Returns the persistentClass

Returns:
the persistentClass

getSessionStrategy

protected final ISessionStrategy getSessionStrategy()
Returns the sessionStrategy

Returns:
the sessionStrategy

getTransactionStrategy

protected final ITransactionStrategy getTransactionStrategy()
Returns the transactionStrategy

Returns:
the transactionStrategy


Copyright © 2007-2009. All Rights Reserved.