org.openbp.server.persistence.hibernate
Class HibernatePersistenceContext

java.lang.Object
  extended by org.openbp.server.persistence.BasicPersistenceContext
      extended by org.openbp.server.persistence.hibernate.HibernatePersistenceContext
All Implemented Interfaces:
PersistenceContext

public class HibernatePersistenceContext
extends BasicPersistenceContext

The Hibernate persistence context represents a Hibernate session.

Author:
Heiko Erhardt

Field Summary
protected  org.hibernate.Session session
          Underlying Hibernate session
protected  org.hibernate.SessionFactory sessionFactory
          Hibernate session factory
 
Constructor Summary
HibernatePersistenceContext(org.hibernate.SessionFactory sessionFactory, BasicPersistenceContextProvider provider)
          Default constructor.
 
Method Summary
 void deleteObject(java.lang.Object o)
          Deletes an object from persistent storage.
 void doBeginTransaction()
           
 void doCommitTransaction()
           
 void doRollbackTransaction()
           
 void evict(java.lang.Object obj)
          Removes the given persistent object from the session cache.
 java.util.Iterator executeSelect(java.lang.String sql, int maxResults)
          Runs the given SQL select statement.
 int executeUpdateOrDelete(java.lang.String sql)
          Runs the given SQL update or delete statement.
 java.lang.Object findById(java.lang.Object id, java.lang.Class entityClass)
          Finds an object by its primary key.
 void flush()
          Forces the underlying persistence session to flush.
 org.hibernate.Session getHibernateSession()
          Gets the underlying hibernate session object.
 java.lang.Object getObjectId(java.lang.Object obj)
          Gets the primary key of the given object.
 boolean isPersistentObject(java.lang.Object obj)
          Determines if the given class is managed by this persistence context.
 boolean isTransactionActive()
           
 java.lang.Object merge(java.lang.Object obj)
          Merges the (transient) given object with the current session and returns the persistent object.
 java.lang.Object refreshObject(java.lang.Object obj)
          Refreshes the given persistent object.
 void release()
          Releases the context.
 java.util.Iterator runQuery(PersistenceQuery query)
          Returns a list of the objects of a particular type that match the given criterion.
 java.lang.Object saveObject(java.lang.Object o)
          Saves the given object to the persistent storage.
 
Methods inherited from class org.openbp.server.persistence.BasicPersistenceContext
beginTransaction, commitTransaction, createEntity, createLoggedException, createQuery, determineEntityClass, getPersistenceContextProvider, isTransactional, rollbackTransaction, setPersistenceContextProvider, setTransactional, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

sessionFactory

protected final org.hibernate.SessionFactory sessionFactory
Hibernate session factory


session

protected org.hibernate.Session session
Underlying Hibernate session

Constructor Detail

HibernatePersistenceContext

public HibernatePersistenceContext(org.hibernate.SessionFactory sessionFactory,
                                   BasicPersistenceContextProvider provider)
Default constructor.

Parameters:
sessionFactory - Hibernate session factory object
provider - Persistence context provider
Method Detail

release

public void release()
Releases the context. The underlying database session will be closed.

Specified by:
release in interface PersistenceContext
Overrides:
release in class BasicPersistenceContext

isPersistentObject

public boolean isPersistentObject(java.lang.Object obj)
Determines if the given class is managed by this persistence context.

Parameters:
obj - Object to check (usually an object implementing PeristentObject)

getObjectId

public java.lang.Object getObjectId(java.lang.Object obj)
                             throws PersistenceException
Gets the primary key of the given object.

Parameters:
obj - The object
Returns:
The primary key or null if the object is not persistent or if it has not been inserted into the database yet
Throws:
PersistenceException - On error

merge

public java.lang.Object merge(java.lang.Object obj)
                       throws PersistenceException
Merges the (transient) given object with the current session and returns the persistent object. Reloads the object values from the database.

Parameters:
obj - Object to refresh
Returns:
The merged object (might be the same or another instance of the object)
Throws:
PersistenceException - On error

refreshObject

public java.lang.Object refreshObject(java.lang.Object obj)
                               throws PersistenceException
Refreshes the given persistent object. Reloads the object values from the database.

Parameters:
obj - Object to refresh
Returns:
The refreshed object (might be the same or another instance of the object)
Throws:
PersistenceException - On error

evict

public void evict(java.lang.Object obj)
           throws PersistenceException
Removes the given persistent object from the session cache.

Parameters:
obj - Object to evict
Throws:
PersistenceException - On error

findById

public java.lang.Object findById(java.lang.Object id,
                                 java.lang.Class entityClass)
                          throws PersistenceException
Finds an object by its primary key.

Parameters:
id - Primary key
entityClass - Type of object to lookup (usually a class implementing PeristentObject)
Returns:
The object or null if no such object can be found
Throws:
PersistenceException - On error

runQuery

public java.util.Iterator runQuery(PersistenceQuery query)
                            throws PersistenceException
Returns a list of the objects of a particular type that match the given criterion.

Parameters:
query - Query to run
Returns:
The list or null
Throws:
PersistenceException - On error

saveObject

public java.lang.Object saveObject(java.lang.Object o)
                            throws PersistenceException
Saves the given object to the persistent storage. According to the persistence state of the object, performs an update or insert.

Parameters:
o - Object to insert or update
Returns:
The updated object; usually this will be identical to the argument object, except if the object already exists in persistent storage and a merge operation needs to be performed.
Throws:
PersistenceException - On error

deleteObject

public void deleteObject(java.lang.Object o)
                  throws PersistenceException
Deletes an object from persistent storage.

Parameters:
o - Object to delete
Throws:
PersistenceException - On error

executeUpdateOrDelete

public int executeUpdateOrDelete(java.lang.String sql)
                          throws PersistenceException
Runs the given SQL update or delete statement.

Parameters:
sql - An SQL update statement
Returns:
The number of rows affected.
Throws:
PersistenceException - On error

executeSelect

public java.util.Iterator executeSelect(java.lang.String sql,
                                        int maxResults)
                                 throws PersistenceException
Runs the given SQL select statement.

Parameters:
sql - SQL query to run
maxResults - Maximum number of result rows or 0 for unlimited
Returns:
A list of result elements (contains Object or Object[] elements, depending if this was a single column or multi-column query)
Throws:
PersistenceException - On error

isTransactionActive

public boolean isTransactionActive()
                            throws PersistenceException
Throws:
PersistenceException

doBeginTransaction

public void doBeginTransaction()
                        throws PersistenceException
Throws:
PersistenceException

doRollbackTransaction

public void doRollbackTransaction()
                           throws PersistenceException
Throws:
PersistenceException

doCommitTransaction

public void doCommitTransaction()
                         throws PersistenceException
Throws:
PersistenceException

flush

public void flush()
           throws PersistenceException
Forces the underlying persistence session to flush. A flush will synchronize the underlying persistent storage with the object state in memory. However, a flush operation will not change the transaction state.

Throws:
PersistenceException - On error performing the operations on the persistent storage

getHibernateSession

public org.hibernate.Session getHibernateSession()
Gets the underlying hibernate session object.



Copyright © 2011. All Rights Reserved.