net.databinder.models.hib
Class HibernateObjectModel

java.lang.Object
  extended by net.databinder.models.LoadableWritableModel
      extended by net.databinder.models.hib.HibernateObjectModel
All Implemented Interfaces:
java.io.Serializable, BindingModel, org.apache.wicket.IClusterable, org.apache.wicket.model.IDetachable, org.apache.wicket.model.IModel

public class HibernateObjectModel
extends LoadableWritableModel
implements BindingModel

Model loaded and persisted by Hibernate. This central Databinder class can be initialized with an entity ID, different types of queries, or an existing persistent object. As a writable Wicket model, the object it contains may be swapped at any time for a different persistent object, a Serializable object, or null.

Author:
Nathan Hamblen
See Also:
Serialized Form

Constructor Summary
HibernateObjectModel()
          Construct with no object.
HibernateObjectModel(java.lang.Class objectClass)
          Constructor for a model with no existing persistent object.
HibernateObjectModel(java.lang.Class objectClass, CriteriaBuilder criteriaBuilder)
          Construct with a class and criteria binder that return exactly one result.
HibernateObjectModel(java.lang.Class objectClass, java.io.Serializable entityId)
          Create a model bound to the given class and entity id.
HibernateObjectModel(java.lang.Object persistentObject)
          Construct with an entity.
HibernateObjectModel(QueryBuilder queryBuilder)
          Construct with a query builder that returns exactly one result, used for custom query objects.
HibernateObjectModel(java.lang.String queryString, QueryBinder queryBinder)
          Construct with a query and binder that return exactly one result.
 
Method Summary
 void checkBinding()
          Checks if the model is retaining an object this has since become a persistent entity.
 void clearPersistentObject()
          Deprecated. use unbind()
 boolean equals(java.lang.Object obj)
          Compares contained objects if present, otherwise calls super-implementation.
 java.lang.Object getFactoryKey()
           
 java.io.Serializable getIdentifier()
           
 boolean getRetainUnsaved()
          When retainUnsaved is true (the default) and the model is not bound, the model object must be Serializable as it is retained in the Web session between requests.
 java.io.Serializable getVersion()
          Uses version annotation to find version for this Model's object.
 int hashCode()
           
 boolean isBound()
          "bound" models are those that can be loaded from persistent storage by a known id or query.
protected  java.lang.Object load()
          Load the object through Hibernate, contruct a new instance if it is not bound to an id, or use unsaved retained object.
 HibernateObjectModel setFactoryKey(java.lang.Object key)
          Set a factory key other than the default (null).
 void setObject(java.lang.Object object)
          Change the persistent object contained in this model.
 void setRetainUnsaved(boolean retainUnsaved)
          Unsaved Serializable objects can be retained between requests.
 void unbind()
          Disassociates this object from any persistent object, but retains the class for constructing a blank copy if requested.
 
Methods inherited from class net.databinder.models.LoadableWritableModel
detach, getObject, isAttached, onAttach, onDetach, setTempModelObject, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.wicket.model.IModel
getObject
 
Methods inherited from interface org.apache.wicket.model.IDetachable
detach
 

Constructor Detail

HibernateObjectModel

public HibernateObjectModel(java.lang.Class objectClass,
                            java.io.Serializable entityId)
Create a model bound to the given class and entity id. If nothing matches the id the model object will be null.

Parameters:
objectClass - class to be loaded and stored by Hibernate
entityId - id of the persistent object

HibernateObjectModel

public HibernateObjectModel(java.lang.Class objectClass)
Constructor for a model with no existing persistent object. This class should be Serializable so that the new object can be stored in the session until it is persisted. If serialization is impossible, call setRetainUnsaved(false) and the object will be discarded and recreated with each request.

Parameters:
objectClass - class to be loaded and stored by Hibernate

HibernateObjectModel

public HibernateObjectModel(java.lang.Object persistentObject)
Construct with an entity.

Parameters:
persistentObject - should be previously persisted or Serializable for temp storage.

HibernateObjectModel

public HibernateObjectModel(java.lang.String queryString,
                            QueryBinder queryBinder)
Construct with a query and binder that return exactly one result. Use this for fetch instructions, scalar results, or if the persistent object ID is not available. Queries that return more than one result will produce exceptions. Queries that return no result will produce a null object.

Parameters:
queryString - query returning one result
queryBinder - bind id or other parameters

HibernateObjectModel

public HibernateObjectModel(java.lang.Class objectClass,
                            CriteriaBuilder criteriaBuilder)
Construct with a class and criteria binder that return exactly one result. Use this for fetch instructions, scalar results, or if the persistent object ID is not available. Criteria that return more than one result will produce exceptions. Criteria that return no result will produce a null object.

Parameters:
objectClass - class of object for root criteria
criteriaBuilder - builder to apply criteria restrictions

HibernateObjectModel

public HibernateObjectModel(QueryBuilder queryBuilder)
Construct with a query builder that returns exactly one result, used for custom query objects. Queries that return more than one result will produce exceptions. Queries that return no result will produce a null object.

Parameters:
queryBuilder - builder to create and bind query object

HibernateObjectModel

public HibernateObjectModel()
Construct with no object. Will return null for getObject().

Method Detail

getFactoryKey

public java.lang.Object getFactoryKey()
Returns:
session factory key, or null for the default factory

setFactoryKey

public HibernateObjectModel setFactoryKey(java.lang.Object key)
Set a factory key other than the default (null).

Parameters:
key - session factory key
Returns:
this, for chaining

setObject

public void setObject(java.lang.Object object)
Change the persistent object contained in this model. Because this method establishes a persistent object ID, queries and binders are removed if present.

Specified by:
setObject in interface org.apache.wicket.model.IModel
Parameters:
object - must be an entity contained in the current Hibernate session, or Serializable, or null

getIdentifier

public java.io.Serializable getIdentifier()

clearPersistentObject

public void clearPersistentObject()
Deprecated. use unbind()


load

protected java.lang.Object load()
Load the object through Hibernate, contruct a new instance if it is not bound to an id, or use unsaved retained object. Returns null if no criteria needed to load or construct an object are available.

Specified by:
load in class LoadableWritableModel

checkBinding

public void checkBinding()
Checks if the model is retaining an object this has since become a persistent entity. If so, the ID is fetched and the reference discarded.


getVersion

public java.io.Serializable getVersion()
Uses version annotation to find version for this Model's object.

Returns:
Persistent storage version number if available, null otherwise

equals

public boolean equals(java.lang.Object obj)
Compares contained objects if present, otherwise calls super-implementation.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object
Returns:
hash of contained object if present, otherwise from super-implementation.

unbind

public void unbind()
Disassociates this object from any persistent object, but retains the class for constructing a blank copy if requested.

Specified by:
unbind in interface BindingModel
See Also:
HibernateObjectModel(Class objectClass)

isBound

public boolean isBound()
"bound" models are those that can be loaded from persistent storage by a known id or query. When bound, this model discards its temporary model object at the end of every request cycle and reloads it via Hiberanate when needed again. When unbound, its behavior is dictated by the value of retanUnsaved.

Specified by:
isBound in interface BindingModel
Returns:
true if information needed to load from Hibernate (identifier, query, or criteria) is present

getRetainUnsaved

public boolean getRetainUnsaved()
When retainUnsaved is true (the default) and the model is not bound, the model object must be Serializable as it is retained in the Web session between requests. See isBound() for more information.

Returns:
true if unsaved objects should be retained between requests.

setRetainUnsaved

public void setRetainUnsaved(boolean retainUnsaved)
Unsaved Serializable objects can be retained between requests.

Parameters:
retainUnsaved - set to true to retain unsaved objects


Copyright © 2005-2008 Nathan Hamblen. All Rights Reserved.