org.riotfamily.riot.hibernate.domain
Class ActiveRecord

java.lang.Object
  extended by org.riotfamily.common.beans.config.ConfigurableBean
      extended by org.riotfamily.riot.hibernate.domain.ActiveRecord
Direct Known Subclasses:
ActiveRecordSupport

public abstract class ActiveRecord
extends ConfigurableBean

Use as base class for persistent entity beans if you prefer the active record pattern.

Since:
8.0
Author:
Alf Werder [alf dot werder at artundweise dot de]

Nested Class Summary
protected static interface ActiveRecord.ForEachCallback<T>
           
 
Constructor Summary
ActiveRecord()
           
 
Method Summary
protected static org.hibernate.Query createQuery(String hql, Object... params)
          Creates a HQL Query, binding a number of values to "?"
 void delete()
          Removes this persistent instance from the data store.
protected static
<T> List<T>
find(String hql, Object... params)
          Executes a HQL query, binding a number of values to "?"
protected static
<T> void
forEach(ActiveRecord.ForEachCallback<T> callback, String hql, Object... params)
          Convenience method to perform some code for every single result obtained by the given query.
protected static org.hibernate.Session getSession()
          Returns the Hibernate Session to use for persistence operations.
protected static org.hibernate.SessionFactory getSessionFactory()
           
protected static
<T> T
load(Class<T> clazz, Serializable id)
          Returns the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
protected static
<T> T
load(String hql, Object... params)
          Convenience method to return a single instance that matches the query, or null if the query returns no results.
 void lock(org.hibernate.LockMode lockMode)
          Obtains the specified lock level upon this persistent instance.
<T> T
merge()
          Copies the state of this object onto the persistent object with the same identifier.
 void save()
          Persists this transient instance, first assigning a generated identifier.
 void update()
          Updates the persistent instance with the same identifier as this detached instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActiveRecord

public ActiveRecord()
Method Detail

getSessionFactory

protected static final org.hibernate.SessionFactory getSessionFactory()

save

public final void save()
Persists this transient instance, first assigning a generated identifier.

Why is this method final? Changing the implementation of this method could seriously defect the whole persistence mechanism.

See Also:
Session.save(Object)

merge

public final <T> T merge()
Copies the state of this object onto the persistent object with the same identifier.

Why is this method final? Changing the implementation of this method could seriously defect the whole persistence mechanism.

Returns:
a detached instance with state to be copied
See Also:
Session.merge(Object)

update

public final void update()
Updates the persistent instance with the same identifier as this detached instance. If there is a persistent instance with the same identifier, an exception is thrown.

See Also:
Session.update(Object)

delete

public final void delete()
Removes this persistent instance from the data store.

Why is this method final? Changing the implementation of this method could seriously defect the whole persistence mechanism.

See Also:
Session.delete(Object)

load

protected static <T> T load(Class<T> clazz,
                            Serializable id)
Returns the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.

Under the hood Session.get(Class, java.io.Serializable) is used, not Session.load(Class, java.io.Serializable) as one might expect because of this method's name. See Hibernate documentation for a detailed discussion of the difference.

Parameters:
clazz - a persistent class
id - an identifier
Returns:
a persistent instance or null
See Also:
Session.get(Class, java.io.Serializable), Session.load(Class, java.io.Serializable)

lock

public final void lock(org.hibernate.LockMode lockMode)
Obtains the specified lock level upon this persistent instance.

Why is this method final? Changing the implementation of this method could seriously defect the whole persistence mechanism.

Parameters:
lockMode - a LockMode
See Also:
Session.lock(Object, LockMode)

getSession

protected static final org.hibernate.Session getSession()
Returns the Hibernate Session to use for persistence operations.

Returns:
the Hibernate Session

createQuery

protected static org.hibernate.Query createQuery(String hql,
                                                 Object... params)
Creates a HQL Query, binding a number of values to "?" parameters in the query string.

Parameters:
hql - a query expressed in Hibernate's query language that may contain one or more '?' parameter placeholders
params - the values of the parameters
Returns:
a newly created Query

find

protected static <T> List<T> find(String hql,
                                  Object... params)
Executes a HQL query, binding a number of values to "?" parameters in the query string.

Parameters:
hql - a query expressed in Hibernate's query language that may contain one or more '?' parameter placeholders
params - the values of the parameters
Returns:
a List containing the results of the query execution

load

protected static <T> T load(String hql,
                            Object... params)
                 throws org.hibernate.NonUniqueResultException
Convenience method to return a single instance that matches the query, or null if the query returns no results.

Parameters:
hql - a query expressed in Hibernate's query language that may contain one or more '?' parameter placeholders
params - the values of the parameters
Returns:
the single result or null
Throws:
org.hibernate.NonUniqueResultException - if there is more than one matching result

forEach

protected static <T> void forEach(ActiveRecord.ForEachCallback<T> callback,
                                  String hql,
                                  Object... params)
Convenience method to perform some code for every single result obtained by the given query. It's save to call this method on a result set of arbitrary length.

This is achieved by two design decisions: Query.scroll() is used to obtain a scrollable result set and after processing a single result, Session.evict(Object) is called for the entity just processed. So, implementors of ActiveRecord.ForEachCallback must be aware that only the currently processed entity is attached to the Hibernate Session.

Type Parameters:
T - a mapped type or Object[] if the query returns more than 1 column per row
Parameters:
callback - a ActiveRecord.ForEachCallback to call for every entity returned by the given query
hql - a query expressed in Hibernate's query language that may contain one or more '?' parameter placeholders
params - the values of the parameters
See Also:
Query.scroll(), Session.evict(Object), ActiveRecord.ForEachCallback