br.com.arsmachina.dao.hibernate
Class ReadableDAOImpl<T,K extends Serializable>

java.lang.Object
  extended by br.com.arsmachina.dao.hibernate.BaseHibernateDAO<T,K>
      extended by br.com.arsmachina.dao.hibernate.ReadableDAOImpl<T,K>
Type Parameters:
T - the entity class related to this DAO.
K - the type of the field that represents the entity class' primary key.
All Implemented Interfaces:
br.com.arsmachina.dao.ReadableDAO<T,K>

public abstract class ReadableDAOImpl<T,K extends Serializable>
extends BaseHibernateDAO<T,K>
implements br.com.arsmachina.dao.ReadableDAO<T,K>

ReadableDAO implementation using Hibernate. All methods use BaseHibernateDAO.getSession() to get Session.

Author:
Thiago H. de Paula Figueiredo

Field Summary
static br.com.arsmachina.dao.SortCriterion[] EMPTY_SORTING_CRITERIA
          A SortCriterion array with no elements.
 
Constructor Summary
ReadableDAOImpl(Class<T> clasz, org.hibernate.SessionFactory sessionFactory)
          Constructor that takes a Class and a SessionFactory.
ReadableDAOImpl(org.hibernate.SessionFactory sessionFactory)
          Constructor that takes a Class and a SessionFactory.
 
Method Summary
protected  void addSortCriteria(org.hibernate.Criteria criteria)
          Adds the default sort criteria to a Criteria instance.
 void addSortCriteria(org.hibernate.Criteria criteria, br.com.arsmachina.dao.SortCriterion... sortCriteria)
          Adds sortCriteria to a Criteria instance.
 int countAll()
           
 org.hibernate.Criteria createCriteria()
          Creates a Criteria for this entity class.
 org.hibernate.Criteria createCriteria(int firstIndex, int maximumResults, br.com.arsmachina.dao.SortCriterion... sortCriteria)
          Creates a Criteria for this entity class with given sort criteria, first result index and maximum number of results.
 org.hibernate.Criteria createCriteria(br.com.arsmachina.dao.SortCriterion... sortCriteria)
          Creates a Criteria for this entity class with given sort criteria.
 org.hibernate.criterion.Example createExample(T entity)
          Used by findByExample(Object) to create an Example instance.
 List<T> findAll()
          Returns all the entity class' objects.
 List<T> findAll(int firstResult, int maximumResults, br.com.arsmachina.dao.SortCriterion... sortingConstraints)
          If sortingConstraints is null or empty, this implementation sort the results by the SortCriterions returned by #getDefaultSortCriterions().
 List<T> findByExample(T example)
           
 T findById(K id)
           
 List<T> findByIds(K... ids)
           
 String getDefaultHqlOrderBy()
          Returns the value of the defaultHqlOrderBy property.
 br.com.arsmachina.dao.SortCriterion[] getDefaultSortCriteria()
          Returns the default SortCriterions to be used to sort the objects lists returned by methods like findAll() and findAll(int, int, SortCriterion...) when no sorting constraints are given.
 T reattach(T object)
          Reattaches the object to the current Session using Session.lock(object, LockMode.NONE) and then returns the object.
 void refresh(T object)
           
static String toHqlOrderBy(br.com.arsmachina.dao.SortCriterion... sortCriteria)
          Returns a HQL order by clause given some SortCriterions.
 
Methods inherited from class br.com.arsmachina.dao.hibernate.BaseHibernateDAO
getClassMetadata, getEntityClass, getPrimaryKeyPropertyName, getSession, getSessionFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_SORTING_CRITERIA

public static final br.com.arsmachina.dao.SortCriterion[] EMPTY_SORTING_CRITERIA
A SortCriterion array with no elements.

Constructor Detail

ReadableDAOImpl

public ReadableDAOImpl(org.hibernate.SessionFactory sessionFactory)
Constructor that takes a Class and a SessionFactory.

Parameters:
clasz - a Class.
sessionFactory - a SessionFactory. It cannot be null.

ReadableDAOImpl

public ReadableDAOImpl(Class<T> clasz,
                       org.hibernate.SessionFactory sessionFactory)
Constructor that takes a Class and a SessionFactory.

Parameters:
clasz - a Class.
sessionFactory - a SessionFactory. It cannot be null.
Method Detail

toHqlOrderBy

public static final String toHqlOrderBy(br.com.arsmachina.dao.SortCriterion... sortCriteria)
Returns a HQL order by clause given some SortCriterions.

Parameters:
sortCriteria - SortCriterion instances.
Returns:
a String.

countAll

public int countAll()
Specified by:
countAll in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
See Also:
ReadableDAO.countAll()

findAll

public List<T> findAll()
Returns all the entity class' objects. They are sorted according to #getDefaultSortCriterions().

Specified by:
findAll in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
See Also:
ReadableDAO.findAll(), #getDefaultSortCriterions()

findById

public T findById(K id)
Specified by:
findById in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
See Also:
ReadableDAO.findById(java.io.Serializable)

findByIds

public List<T> findByIds(K... ids)
Specified by:
findByIds in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
See Also:
br.com.arsmachina.dao.ReadableDAO#findByIds(K[])

findByExample

public List<T> findByExample(T example)
Specified by:
findByExample in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
See Also:
ReadableDAO.findByExample(java.lang.Object)

refresh

public void refresh(T object)
Specified by:
refresh in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
See Also:
br.com.arsmachina.dao.WriteableDAO#refresh(java.lang.Object)

findAll

public List<T> findAll(int firstResult,
                       int maximumResults,
                       br.com.arsmachina.dao.SortCriterion... sortingConstraints)
If sortingConstraints is null or empty, this implementation sort the results by the SortCriterions returned by #getDefaultSortCriterions().

Specified by:
findAll in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
See Also:
ReadableDAO.findAll(int, int, br.com.arsmachina.dao.SortCriterion[])

reattach

public T reattach(T object)
Reattaches the object to the current Session using Session.lock(object, LockMode.NONE) and then returns the object.

Specified by:
reattach in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
Parameters:
a - T.
Returns:
object.
See Also:
ReadableDAO.reattach(java.lang.Object)

addSortCriteria

public final void addSortCriteria(org.hibernate.Criteria criteria,
                                  br.com.arsmachina.dao.SortCriterion... sortCriteria)
Adds sortCriteria to a Criteria instance.

Parameters:
criteria - a Criteria. It cannot be null.
sortCriteria - a SortCriterion.... It cannot be null.

addSortCriteria

protected void addSortCriteria(org.hibernate.Criteria criteria)
Adds the default sort criteria to a Criteria instance. This method just does addSortCriteria(criteria, getDefaultSortCriteria());.

Parameters:
criteria - a Criteria. It cannot be null.

getDefaultSortCriteria

public br.com.arsmachina.dao.SortCriterion[] getDefaultSortCriteria()
Returns the default SortCriterions to be used to sort the objects lists returned by methods like findAll() and findAll(int, int, SortCriterion...) when no sorting constraints are given. This implementation returns EMPTY_SORTING_CRITERIA.

Specified by:
getDefaultSortCriteria in interface br.com.arsmachina.dao.ReadableDAO<T,K extends Serializable>
Returns:
a SortCriterion array. It cannot be null.

createCriteria

public org.hibernate.Criteria createCriteria()
Creates a Criteria for this entity class.

Returns:
a Criteria.

createCriteria

public org.hibernate.Criteria createCriteria(br.com.arsmachina.dao.SortCriterion... sortCriteria)
Creates a Criteria for this entity class with given sort criteria.

Returns:
a Criteria.

createCriteria

public org.hibernate.Criteria createCriteria(int firstIndex,
                                             int maximumResults,
                                             br.com.arsmachina.dao.SortCriterion... sortCriteria)
Creates a Criteria for this entity class with given sort criteria, first result index and maximum number of results.

Returns:
a Criteria.

createExample

public org.hibernate.criterion.Example createExample(T entity)
Used by findByExample(Object) to create an Example instance.

Returns:
an Example.

getDefaultHqlOrderBy

public final String getDefaultHqlOrderBy()
Returns the value of the defaultHqlOrderBy property.

Returns:
a String.


Copyright © 2008-2009 Thiago H. de Paula Figueiredo. All Rights Reserved.