cz.muni.fi.pclis.commons.dao
Class GenericHibernateDao<T extends DomainObject>

java.lang.Object
  extended by cz.muni.fi.pclis.commons.dao.GenericHibernateDao<T>
All Implemented Interfaces:
GenericDao<T>
Direct Known Subclasses:
ContractAdditionDaoHibernate, CourseDaoHibernate, ExpectationPointDaoHibernate, FeedbackDaoHibernate, FeedbackTopicsDaoHibernate, LearningContractDaoHibernate, LearningContractsBehaviourDaoHibernate, LearningExpectationsBehaviourDaoHibernate, OnlineConsultationDaoHibernate, OnlineConsultationRulesDaoHibernate, PeerEvaluationDaoHibernate, ProjectPackageDaoHibernate, ScenarioCourseRelationDaoHibernate, SelfEvaluationAssignmentDaoHibernate, SelfEvaluationDaoHibernate, SeminarDaoHibernate, StudentEvaluationAssignmentDaoHibernate, StudentEvaluationDaoHibernate, TeamEvaluationAssignmentDaoHibernate, TeamEvaluationDaoHibernate, TeamProjectDaoHibernate, TermDaoHibernate, UploadedFileDaoHibernate, UserBlogLocationDaoHibernate, UserDaoHibernate, UserToEducatableRelationDaoHibernate

@Repository
public class GenericHibernateDao<T extends DomainObject>
extends Object
implements GenericDao<T>

Generic hibernate implementation of GenericDao

Author:
Ľuboš Pecho

Field Summary
private  javax.persistence.EntityManager entityManager
          Used Entity Manager
private  boolean FIND_LOCK_DEFAULT
           
private  Class<? extends T> persistentClass
          Persistent class of this DAO
 
Constructor Summary
  GenericHibernateDao()
          Creates new GenericHibernateDao with the \ persistentClass being the class of generalized type ().
protected GenericHibernateDao(Class<? extends T> persistentClass)
          Creates new GenericHibernateDao
 
Method Summary
 boolean contains(T entity)
          Returns true if the entity is persisted
 boolean containsById(Long id)
          Returns true if entity with given id is persisted
 void create(T entity)
          Persists the entity
protected  org.hibernate.Query createHibernateQuery(String query)
          creates hibernate query
protected  javax.persistence.Query createJPAQuery(String query)
          creates JPA query
protected  javax.persistence.Query createNamedQuery(String name)
          Creates named query
protected  javax.persistence.Query createQuery(String query)
          creates query
protected  List<T> createResultList()
          Creates the new result list used to store dao method results.
protected  Set<T> createResultSet()
          Creates the new result set used to store dao method results.
 void delete(T entity)
          Deletes the entity from the database
 javax.persistence.EntityManager em()
          Uses the entity manager for internal purposes.
 List<T> findAll()
          Returns list of all entities in natural sort order.
protected  List<T> findByCriteria(org.hibernate.criterion.Criterion... criterion)
          Searches through database by given criteria
 T findById(long id)
          Finds the object by its id
 T findById(long id, boolean lock)
          Finds the object by its id
 List<T> findByIDs(long[] IDs, boolean lck)
          Searches for the objects by their respective id's.
 void flush()
          flushes the cachce to the database
protected  String getEntityClassSimpleName()
          Gets the simple name of entity class
 javax.persistence.EntityManager getEntityManager()
          Gets the entityManager.
protected  org.hibernate.ejb.HibernateEntityManager getHibernateEntityManager()
           
protected  org.hibernate.Session getHibernateSession()
           
 Class<? extends T> getPersistentClass()
          Returns the type of the implemented dato
protected  T getQuerySingleOrNullResult(javax.persistence.Query query)
          Returns single result of the query or null value if the result is empty
protected  T getSingleResult(javax.persistence.Query query)
          Returns the single result of the query
<T> T
refresh(T entity)
          Refreshes the entity from the database
 List runNamedQuery(String name, Map<String,Object> params)
          Runs named query
 Object runNamedQueryForSingleResult(String name, Map<String,Object> params)
          Runs named query which should return single result
 T save(T entity)
          Merge the state of the given entity into the current dao context.
 void setEntityManager(javax.persistence.EntityManager entityManager)
           
 Set<T> toSet(Collection col)
          Utility.
static
<T> Set<T>
toSet(Collection col, Class<? extends T> cls)
          Utility.
 T update(T entity)
          Update the persistent instance with the identifier of the given detached instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FIND_LOCK_DEFAULT

private final boolean FIND_LOCK_DEFAULT
See Also:
Constant Field Values

persistentClass

private Class<? extends T extends DomainObject> persistentClass
Persistent class of this DAO


entityManager

private javax.persistence.EntityManager entityManager
Used Entity Manager

Constructor Detail

GenericHibernateDao

public GenericHibernateDao()
Creates new GenericHibernateDao with the \ persistentClass being the class of generalized type ().


GenericHibernateDao

protected GenericHibernateDao(Class<? extends T> persistentClass)
Creates new GenericHibernateDao

Parameters:
persistentClass -
Method Detail

setEntityManager

public void setEntityManager(javax.persistence.EntityManager entityManager)
Parameters:
entityManager - Sets entityManager.

getEntityManager

public javax.persistence.EntityManager getEntityManager()
Gets the entityManager.

Returns:
Returns entityManager.

em

public javax.persistence.EntityManager em()
Uses the entity manager for internal purposes.

If the entityManager is not set \ ( by none possible way ( ioc / manual setting)) \ it throws the detailed IllegalStateException.

Returns:
Returns entityManager.
Throws:
IllegalStateException - if the entityManager is not set.

getHibernateEntityManager

protected org.hibernate.ejb.HibernateEntityManager getHibernateEntityManager()
Returns:
Returns Hibernate entityManager.

getHibernateSession

protected org.hibernate.Session getHibernateSession()
Returns:
Returns Hibernate session

getPersistentClass

public Class<? extends T> getPersistentClass()
Description copied from interface: GenericDao
Returns the type of the implemented dato

Specified by:
getPersistentClass in interface GenericDao<T extends DomainObject>
Returns:
Returns persistentClass.

getEntityClassSimpleName

protected final String getEntityClassSimpleName()
Gets the simple name of entity class

Returns:

findById

public T findById(long id)
Finds the object by its id

Specified by:
findById in interface GenericDao<T extends DomainObject>
Parameters:
id - of the object
Returns:

findById

public T findById(long id,
                  boolean lock)
Description copied from interface: GenericDao
Finds the object by its id

Specified by:
findById in interface GenericDao<T extends DomainObject>
Parameters:
id - of the object
lock - if the object should be locked in the database
Returns:

findByIDs

public List<T> findByIDs(long[] IDs,
                         boolean lck)
Searches for the objects by their respective id's. This method follows it's common sense.

Specified by:
findByIDs in interface GenericDao<T extends DomainObject>
Returns:
objects

findAll

@Transactional
public List<T> findAll()
Description copied from interface: GenericDao
Returns list of all entities in natural sort order.

Specified by:
findAll in interface GenericDao<T extends DomainObject>
Returns:

save

public T save(T entity)
Description copied from interface: GenericDao
Merge the state of the given entity into the current dao context.

Specified by:
save in interface GenericDao<T extends DomainObject>
Returns:

update

public T update(T entity)
Update the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown. This operation cascades to associated instances if the association is mapped with cascade="save-update".

Specified by:
update in interface GenericDao<T extends DomainObject>
Parameters:
entity - a detached instance containing updated state
See Also:
Session.update(Object)

create

public void create(T entity)
Persists the entity

Specified by:
create in interface GenericDao<T extends DomainObject>

contains

public boolean contains(T entity)
Returns true if the entity is persisted

Specified by:
contains in interface GenericDao<T extends DomainObject>
Returns:

containsById

public boolean containsById(Long id)
Returns true if entity with given id is persisted

Specified by:
containsById in interface GenericDao<T extends DomainObject>
Returns:

delete

public void delete(T entity)
Deletes the entity from the database

Specified by:
delete in interface GenericDao<T extends DomainObject>

refresh

public <T> T refresh(T entity)
Refreshes the entity from the database

Specified by:
refresh in interface GenericDao<T extends DomainObject>
Returns:

flush

public void flush()
flushes the cachce to the database

Specified by:
flush in interface GenericDao<T extends DomainObject>

findByCriteria

protected List<T> findByCriteria(org.hibernate.criterion.Criterion... criterion)
Searches through database by given criteria

Parameters:
criterion -
Returns:
result of the search

createNamedQuery

protected final javax.persistence.Query createNamedQuery(String name)
Creates named query

Parameters:
name -
Returns:

createQuery

protected final javax.persistence.Query createQuery(String query)
creates query

Parameters:
query -
Returns:

createJPAQuery

protected final javax.persistence.Query createJPAQuery(String query)
creates JPA query

Parameters:
query -
Returns:

createHibernateQuery

protected final org.hibernate.Query createHibernateQuery(String query)
creates hibernate query

Parameters:
query -
Returns:

getQuerySingleOrNullResult

protected final T getQuerySingleOrNullResult(javax.persistence.Query query)
Returns single result of the query or null value if the result is empty

Parameters:
query -
Returns:
Throws:
javax.persistence.NonUniqueResultException - if the query returns more than one result

runNamedQuery

public List runNamedQuery(String name,
                          Map<String,Object> params)
Runs named query

Specified by:
runNamedQuery in interface GenericDao<T extends DomainObject>
Parameters:
name - of the query
Returns:

runNamedQueryForSingleResult

public Object runNamedQueryForSingleResult(String name,
                                           Map<String,Object> params)
Runs named query which should return single result

Specified by:
runNamedQueryForSingleResult in interface GenericDao<T extends DomainObject>
Parameters:
name - of the query
Returns:

toSet

public Set<T> toSet(Collection col)
Utility. Conversion of collection to set.

Parameters:
col -
Returns:

toSet

public static <T> Set<T> toSet(Collection col,
                               Class<? extends T> cls)
Utility. Conversion of collection to set.

Parameters:
col -
Returns:

createResultList

protected List<T> createResultList()
Creates the new result list used to store dao method results.


createResultSet

protected Set<T> createResultSet()
Creates the new result set used to store dao method results.


getSingleResult

protected T getSingleResult(javax.persistence.Query query)
Returns the single result of the query

Parameters:
query -
Returns:


Copyright © 2009-2010 Faculty of Informatics, Masaryk university, Brno. All Rights Reserved.