it.openutils.dao.hibernate
Class HibernateDAOImpl<T,K extends java.io.Serializable>

java.lang.Object
  extended by org.springframework.dao.support.DaoSupport
      extended by org.springframework.orm.hibernate3.support.HibernateDaoSupport
          extended by it.openutils.dao.hibernate.HibernateDAOImpl<T,K>
Type Parameters:
T - Persistence class
K - Object Key
All Implemented Interfaces:
HibernateDAO<T,K>, org.springframework.beans.factory.InitializingBean

public abstract class HibernateDAOImpl<T,K extends java.io.Serializable>
extends org.springframework.orm.hibernate3.support.HibernateDaoSupport
implements HibernateDAO<T,K>

Base Hibernate DAO.

Version:
$Id: HibernateDAOImpl.java 830 2008-06-09 16:46:26Z fcarone $
Author:
Fabrizio Giustina

Field Summary
 
Fields inherited from class org.springframework.dao.support.DaoSupport
logger
 
Constructor Summary
HibernateDAOImpl()
          Instantiates a new DAO instance
HibernateDAOImpl(java.lang.Class referenceClass)
          Sets the class of the persistent bean managed by this DAO
 
Method Summary
 boolean delete(K key)
          Used by the base DAO classes but here for your modification.
 void evict(T obj)
          Remove the given object from the Session cache.
 java.util.List<T> find(java.lang.String query)
          Execute a query.
 java.util.List<T> find(java.lang.String query, java.lang.Object[] obj, org.hibernate.type.Type[] type)
          Execute a query.
 java.util.List<T> find(java.lang.String query, java.lang.Object obj, org.hibernate.type.Type type)
          Execute a query.
 java.util.List<T> findAll()
          Return all objects related to the implementation of this DAO with no filter.
 java.util.List<T> findAll(org.hibernate.criterion.Order[] orderProperties)
          Return all objects related to the implementation of this DAO with no filter.
 java.util.List<T> findAll(org.hibernate.criterion.Order[] orderProperties, java.util.List<org.hibernate.criterion.Criterion> criteria)
          Return all objects related to the implementation of this DAO with no filter.
protected  java.util.List<?> findByNamedQuery(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> params, java.lang.Integer maxResults)
          Obtain an instance of Query for a named query string defined in the mapping file.
protected  java.util.List<?> findByNamedQuery(java.lang.String name, java.io.Serializable[] params, java.lang.Integer maxResults)
          Obtain an instance of Query for a named query string defined in the mapping file.
 java.util.List<T> findFiltered(T filter)
          Return all objects related to the implementation of this DAO filtered using properties of the provided instance.
 java.util.List<T> findFiltered(T filter, int maxResults, int page)
          Return all objects related to the implementation of this DAO filtered using properties of the provided instance.
 java.util.List<T> findFiltered(T filter, java.util.Map<java.lang.String,FilterMetadata> metadata)
          Return all objects related to the implementation of this DAO filtered using properties of the provided instance.
 java.util.List<T> findFiltered(T filter, java.util.Map<java.lang.String,FilterMetadata> metadata, int maxResults, int page)
          Return all objects related to the implementation of this DAO filtered using properties of the provided instance.
 java.util.List<T> findFiltered(T filter, org.hibernate.criterion.Order[] orderProperties)
          Return all objects related to the implementation of this DAO filtered using properties of the provided instance.
 java.util.List<T> findFiltered(T filter, org.hibernate.criterion.Order[] customOrder, java.util.Map<java.lang.String,FilterMetadata> metadata, int maxResults, int page)
          Return all objects related to the implementation of this DAO filtered using properties of the provided instance.
 java.util.List<T> findFiltered(T filter, org.hibernate.criterion.Order[] customOrder, java.util.Map<java.lang.String,FilterMetadata> metadata, int maxResults, int page, java.util.List<org.hibernate.criterion.Criterion> additionalCriteria)
          Return all objects related to the implementation of this DAO filtered using properties of the provided instance.
 T findFilteredFirst(T filter)
          Return the first object related to the implementation of this DAO filtered using properties of the provided instance.
 T findFilteredFirst(T filter, java.util.List<org.hibernate.criterion.Criterion> criteria)
          Return the first object related to the implementation of this DAO filtered using properties of the provided instance.
 T findFilteredFirst(T filter, org.hibernate.criterion.Order[] order)
          Return the first object related to the implementation of this DAO filtered using properties of the provided instance.
 java.util.List<?> findFilteredProperties(T filter, org.hibernate.criterion.Order[] customOrder, java.util.Map<java.lang.String,FilterMetadata> metadata, int maxResults, int page, java.util.List<org.hibernate.criterion.Criterion> additionalCriteria, java.util.List<java.lang.String> properties)
          Return properties from all objects related to the implementation of this DAO filtered using properties of the provided instance.
 T get(K key)
          Load object matching the given key and return it.
protected  java.util.Map<java.lang.String,FilterMetadata> getDefaultFilterMetadata()
          Returns the default set of FilterMetadata that will be applied to any query.
protected  org.hibernate.criterion.Order[] getDefaultOrder()
          Return a list of Order object to be used for the default ordering of the collection.
protected  java.util.List<?> getNamedQuery(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> params, int maxResults)
          Deprecated. use the better named findByNamedQuery method
protected  java.util.List<?> getNamedQuery(java.lang.String name, java.io.Serializable[] params, int maxResults)
          Deprecated. use the better named findByNamedQuery method
protected  java.lang.Class<T> getReferenceClass()
          Return the specific Object class that will be used for class-specific implementation of this DAO.
 T load(K key)
          Load object matching the given key and return it.
 T loadIfAvailable(K key)
          Load object matching the given key and return it.
 T merge(T obj)
          Copy the state of the given object onto the persistent object with the same identifier.
 void refresh(T obj)
          Re-read the state of the given instance from the underlying database.
 K save(T obj)
          Persist the given transient instance, first assigning a generated identifier.
 void saveOrUpdate(T obj)
          Used by the base DAO classes but here for your modification Either save() or update() the given instance, depending upon the value of its identifier property.
 void setAopenabled(boolean aopenabled)
          Sets the aopenabled.
protected  void setParameterValue(org.hibernate.Query query, java.lang.String key, java.lang.Object value)
          Convenience method to set paramers in the query given based on the actual object type in passed in as the value.
 void setReferenceClass(java.lang.Class<T> referenceClass)
           
 void update(T obj)
          Used by the base DAO classes but here for your modification Update the persistent state associated with the given identifier.
 
Methods inherited from class org.springframework.orm.hibernate3.support.HibernateDaoSupport
checkDaoConfig, convertHibernateAccessException, createHibernateTemplate, getHibernateTemplate, getSession, getSession, getSessionFactory, releaseSession, setHibernateTemplate, setSessionFactory
 
Methods inherited from class org.springframework.dao.support.DaoSupport
afterPropertiesSet, initDao
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateDAOImpl

public HibernateDAOImpl()
Instantiates a new DAO instance


HibernateDAOImpl

public HibernateDAOImpl(java.lang.Class referenceClass)
Sets the class of the persistent bean managed by this DAO

Parameters:
referenceClass - the class for the bean managed by this DAO
Method Detail

find

public java.util.List<T> find(java.lang.String query)
Execute a query.

Specified by:
find in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
query - a query expressed in Hibernate's query language
Returns:
a distinct list of instances (or arrays of instances)

findAll

public java.util.List<T> findAll()
Return all objects related to the implementation of this DAO with no filter.

Specified by:
findAll in interface HibernateDAO<T,K extends java.io.Serializable>
Returns:
a list of all instances

findAll

public java.util.List<T> findAll(org.hibernate.criterion.Order[] orderProperties)
Return all objects related to the implementation of this DAO with no filter.

Specified by:
findAll in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
orderProperties - desc or asc
Returns:
a list of all instances

findAll

public java.util.List<T> findAll(org.hibernate.criterion.Order[] orderProperties,
                                 java.util.List<org.hibernate.criterion.Criterion> criteria)
Return all objects related to the implementation of this DAO with no filter.

Specified by:
findAll in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
orderProperties - desc or asc
criteria - Additional Criterion conditions
Returns:
a list of all instances

find

public java.util.List<T> find(java.lang.String query,
                              java.lang.Object obj,
                              org.hibernate.type.Type type)
Execute a query.

Specified by:
find in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
query - a query expressed in Hibernate's query language
obj - filter value
type - filter type
Returns:
a distinct list of instances (or arrays of instances)

find

public java.util.List<T> find(java.lang.String query,
                              java.lang.Object[] obj,
                              org.hibernate.type.Type[] type)
Execute a query.

Specified by:
find in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
query - a query expressed in Hibernate's query language
obj - filter values
type - filter types
Returns:
a distinct list of instances (or arrays of instances)

load

public T load(K key)
Load object matching the given key and return it. Throw an exception if not found.

Specified by:
load in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
key - serializable key
Returns:
Object

loadIfAvailable

public T loadIfAvailable(K key)
Load object matching the given key and return it. Lazy object will be initialized.

Specified by:
loadIfAvailable in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
key - serializable key
Returns:
Object

get

public T get(K key)
Load object matching the given key and return it. Lazy object will be initialized.

Specified by:
get in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
key - serializable key
Returns:
Object

saveOrUpdate

public void saveOrUpdate(T obj)
Used by the base DAO classes but here for your modification Either save() or update() the given instance, depending upon the value of its identifier property.

Specified by:
saveOrUpdate in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
obj - Object

update

public void update(T obj)
Used by the base DAO classes but here for your modification Update the persistent state associated with the given identifier. An exception is thrown if there is a persistent instance with the same identifier in the current session.

Specified by:
update in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
obj - a transient instance containing updated state

delete

public boolean delete(K key)
Used by the base DAO classes but here for your modification. Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state.

Specified by:
delete in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
key - key
Returns:
true if the object was successfully deleted, false otherwise

refresh

public void refresh(T obj)
Re-read the state of the given instance from the underlying database. It is inadvisable to use this to implement long-running sessions that span many business tasks. This method is, however, useful in certain special circumstances. For example

Specified by:
refresh in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
obj - Object

evict

public void evict(T obj)
Remove the given object from the Session cache.

Specified by:
evict in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
obj - Object

merge

public T merge(T obj)
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".

The semantics of this method are defined by JSR-220.

Specified by:
merge in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
obj - a detached instance with state to be copied
Returns:
an updated persistent instance

save

public K save(T obj)
Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.)

Specified by:
save in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
obj - Object
Returns:
generated id

findFilteredFirst

public T findFilteredFirst(T filter)
Return the first object related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFilteredFirst in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
Returns:
first object in the collection

findFilteredFirst

public T findFilteredFirst(T filter,
                           org.hibernate.criterion.Order[] order)
Return the first object related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFilteredFirst in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
Returns:
first object in the collection

findFilteredFirst

public T findFilteredFirst(T filter,
                           java.util.List<org.hibernate.criterion.Criterion> criteria)
Return the first object related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFilteredFirst in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
criteria - additional criterion
Returns:
first object in the collection

findFiltered

public java.util.List<T> findFiltered(T filter)
Return all objects related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFiltered in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
Returns:
list of objects

findFiltered

public java.util.List<T> findFiltered(T filter,
                                      org.hibernate.criterion.Order[] orderProperties)
Return all objects related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFiltered in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
orderProperties - the name of the property used for ordering
Returns:
list of objects

findFiltered

public java.util.List<T> findFiltered(T filter,
                                      java.util.Map<java.lang.String,FilterMetadata> metadata)
Return all objects related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFiltered in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
metadata - filter metadata
Returns:
list of objects

findFiltered

public java.util.List<T> findFiltered(T filter,
                                      int maxResults,
                                      int page)
Return all objects related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFiltered in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
maxResults - maximum number of results
page - result page (first result is maxResults * page)
Returns:
list of objects

findFiltered

public java.util.List<T> findFiltered(T filter,
                                      java.util.Map<java.lang.String,FilterMetadata> metadata,
                                      int maxResults,
                                      int page)
Return all objects related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFiltered in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
metadata - filter metadata
maxResults - maximum number of results
page - result page (first result is maxResults * page)
Returns:
list of objects

findFiltered

public java.util.List<T> findFiltered(T filter,
                                      org.hibernate.criterion.Order[] customOrder,
                                      java.util.Map<java.lang.String,FilterMetadata> metadata,
                                      int maxResults,
                                      int page)
Return all objects related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFiltered in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
customOrder - order criterias
metadata - filter metadata
maxResults - maximum number of results
page - result page (first result is maxResults * page)
Returns:
list of objects

findFiltered

public java.util.List<T> findFiltered(T filter,
                                      org.hibernate.criterion.Order[] customOrder,
                                      java.util.Map<java.lang.String,FilterMetadata> metadata,
                                      int maxResults,
                                      int page,
                                      java.util.List<org.hibernate.criterion.Criterion> additionalCriteria)
Return all objects related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFiltered in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
customOrder - order criterias
metadata - filter metadata
maxResults - maximum number of results
page - result page (first result is maxResults * page)
additionalCriteria - additional criteria
Returns:
list of objects

findFilteredProperties

public java.util.List<?> findFilteredProperties(T filter,
                                                org.hibernate.criterion.Order[] customOrder,
                                                java.util.Map<java.lang.String,FilterMetadata> metadata,
                                                int maxResults,
                                                int page,
                                                java.util.List<org.hibernate.criterion.Criterion> additionalCriteria,
                                                java.util.List<java.lang.String> properties)
Return properties from all objects related to the implementation of this DAO filtered using properties of the provided instance.

Specified by:
findFilteredProperties in interface HibernateDAO<T,K extends java.io.Serializable>
Parameters:
filter - an instance of the object with the properties you wish to filter on.
customOrder - order criterias
metadata - filter metadata
maxResults - maximum number of results
page - result page (first result is maxResults * page)
additionalCriteria - additional criteria
properties - properties to be returned
Returns:
list of properties from all objects

getReferenceClass

protected java.lang.Class<T> getReferenceClass()
Return the specific Object class that will be used for class-specific implementation of this DAO.

Returns:
the reference Class

setReferenceClass

public void setReferenceClass(java.lang.Class<T> referenceClass)

getDefaultOrder

protected org.hibernate.criterion.Order[] getDefaultOrder()
Return a list of Order object to be used for the default ordering of the collection.

Returns:
the property name

findByNamedQuery

protected java.util.List<?> findByNamedQuery(java.lang.String name,
                                             java.io.Serializable[] params,
                                             java.lang.Integer maxResults)
Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given.

Parameters:
name - the name of a query defined externally
params - the parameter array
maxResults - max number of results
Returns:
Query

findByNamedQuery

protected java.util.List<?> findByNamedQuery(java.lang.String name,
                                             java.util.Map<java.lang.String,java.lang.Object> params,
                                             java.lang.Integer maxResults)
Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given.

Parameters:
name - the name of a query defined externally
params - the parameter Map
maxResults - max number of results
Returns:
Query

getNamedQuery

@Deprecated
protected java.util.List<?> getNamedQuery(java.lang.String name,
                                                     java.io.Serializable[] params,
                                                     int maxResults)
Deprecated. use the better named findByNamedQuery method

Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given.

Parameters:
name - the name of a query defined externally
params - the parameter array
maxResults - max number of results
Returns:
Query

getNamedQuery

@Deprecated
protected java.util.List<?> getNamedQuery(java.lang.String name,
                                                     java.util.Map<java.lang.String,java.lang.Object> params,
                                                     int maxResults)
Deprecated. use the better named findByNamedQuery method

Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given.

Parameters:
name - the name of a query defined externally
params - the parameter Map
maxResults - max number of results
Returns:
Query

setParameterValue

protected void setParameterValue(org.hibernate.Query query,
                                 java.lang.String key,
                                 java.lang.Object value)
Convenience method to set paramers in the query given based on the actual object type in passed in as the value. You may need to add more functionaly to this as desired (or not use this at all).

Parameters:
query - the Query to set
key - the key name
value - the object to set as the parameter

getDefaultFilterMetadata

protected java.util.Map<java.lang.String,FilterMetadata> getDefaultFilterMetadata()
Returns the default set of FilterMetadata that will be applied to any query. The default implementation doesn't set any default filter, subclasses may override this.

Returns:
map of property name - filter metadata

setAopenabled

public void setAopenabled(boolean aopenabled)
Sets the aopenabled.

Parameters:
aopenabled - the aopenabled to set


Copyright © 2009 Openmind. All Rights Reserved.