edu.nrao.sss.dao
Class HibernateDao

java.lang.Object
  extended by edu.nrao.sss.dao.HibernateDao
Direct Known Subclasses:
HibernateExecBlockDao, HibernateParameterDao, HibernateProgBlockDao, HibernateProjectDao, HibernateProposalDao, HibernateResourceDao, HibernateSchedBlockDao, HibernateSourceDao, HibernateUserDao

public class HibernateDao
extends Object

This is the base class for Data access objects using Hibernate as their persistence mechanism. It provides basic support for creating SessionFactories and Sessions, and begining and commiting transactions. SessionFactories are saved in a static Map<String, SessionFactory> with keys being the file name of the hibernate configuration file used to create the Factory. This is done so that multiple instances of a dao that accesses the same database don't have to repeat the overhead of creating a SessionFactory. You can also specify a Hibernate Interceptor to be used when creating sessions. Note: This class is not threadsafe! To implement threadsafety, have your subclass synchronize appropriate access methods


Field Summary
protected static Map<String,org.hibernate.SessionFactory> factories
           
 
Constructor Summary
HibernateDao()
          Creates a HibernateDao that uses the default configuration file name and no Interceptor.
HibernateDao(String cfg, org.hibernate.Interceptor i)
          Creates a HibernateDao that uses cfg as it's configuration file and creates Sessions with i as its Interceptor.
 
Method Summary
 void addDaoListener(DaoListener listener)
          Adds a listener that will be notified when the dao has to roll back a transaction
 void beginTransaction()
          If a transaction has not already begun, start one.
 void closeSession()
          If there is an open session, close it.
 void commitTransaction()
          If a transaction is active, commit it.
static void destroyAllFactories()
          This method will close ALL SessionFactories that have been created.
 void destroyFactory()
          This method ONLY closes the factory associated with getConfigurationFileName().
protected  void fireRollbackEvent()
          Notifies all DaoListeners that have been registered that a Rollback Event has occured for this Dao.
 String getConfigurationFileName()
          Returns the Hibernate configuration file name used to create a SessionFactory for this Dao.
 org.hibernate.Interceptor getInterceptor()
          Returns the Interceptor used when creating sessions returned by getSession().
 org.hibernate.Session getSession()
          Returns the currently open Session.
 org.hibernate.SessionFactory getSessionFactory()
          returns a session factory that was configured using the config file pointed to by getConfigurationFileName().
 void removeDaoListeners()
          Removes all dao listeners from the listener list
 void rollbackTransaction()
          If a transaction is active, roll it back.
 void setConfigurationFileName(String cfg)
          Sets the Hibernate configuration file name used to create a SessionFactory for this Dao.
 void setInterceptor(org.hibernate.Interceptor i)
          This method sets the Interceptor that is used to create sessions for this Dao.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factories

protected static Map<String,org.hibernate.SessionFactory> factories
Constructor Detail

HibernateDao

public HibernateDao(String cfg,
                    org.hibernate.Interceptor i)
Creates a HibernateDao that uses cfg as it's configuration file and creates Sessions with i as its Interceptor.

Parameters:
cfg - File name of the main xml hibernate configuration file. Must be on the classpath somewhere. If null, "/hibernate.cfg.xml" is used.
i - Interceptor used when creating Sessions. If null, no Interceptor is used.

HibernateDao

public HibernateDao()
Creates a HibernateDao that uses the default configuration file name and no Interceptor.

See Also:
HibernateDao(String, Interceptor)
Method Detail

getConfigurationFileName

public String getConfigurationFileName()
Returns the Hibernate configuration file name used to create a SessionFactory for this Dao.


setConfigurationFileName

public void setConfigurationFileName(String cfg)
                              throws org.hibernate.HibernateException
Sets the Hibernate configuration file name used to create a SessionFactory for this Dao. If a previous SessionFactory had already been created for this Dao, Transactions are commited, sessions are closed and then the previous SessionFactory is closed.

Throws:
org.hibernate.HibernateException

getSessionFactory

public org.hibernate.SessionFactory getSessionFactory()
                                               throws org.hibernate.HibernateException
returns a session factory that was configured using the config file pointed to by getConfigurationFileName(). If a SessionFactory has already been configured for this configuration file, return the old one.

Throws:
org.hibernate.HibernateException

getSession

public org.hibernate.Session getSession()
                                 throws org.hibernate.HibernateException
Returns the currently open Session. If there is non, one is opened and then returned.

Throws:
org.hibernate.HibernateException

setInterceptor

public void setInterceptor(org.hibernate.Interceptor i)
This method sets the Interceptor that is used to create sessions for this Dao. If i != the old interceptor and a session factory already exists for this configuration, transactions are commited and sessions are closed so that the next session to be asked for will have the interceptor you assigned.

Parameters:
i - can be null.
See Also:
Interceptor

getInterceptor

public org.hibernate.Interceptor getInterceptor()
Returns the Interceptor used when creating sessions returned by getSession(). If null is returned, then no interceptor was used when creating the sessions.

See Also:
Interceptor

closeSession

public void closeSession()
                  throws org.hibernate.HibernateException
If there is an open session, close it.

Throws:
org.hibernate.HibernateException

beginTransaction

public void beginTransaction()
                      throws org.hibernate.HibernateException
If a transaction has not already begun, start one.

Throws:
org.hibernate.HibernateException

commitTransaction

public void commitTransaction()
                       throws org.hibernate.HibernateException
If a transaction is active, commit it. If an exception occurs during the commit, rollbackTransaction() is called.

Throws:
org.hibernate.HibernateException

rollbackTransaction

public void rollbackTransaction()
                         throws org.hibernate.HibernateException
If a transaction is active, roll it back. This method also closes the current session for this thread because all persistent objects related to that session no longer match the data base. Note: if there is no transaction at all, this method does nothing.

Throws:
org.hibernate.HibernateException

destroyFactory

public void destroyFactory()
                    throws org.hibernate.HibernateException
This method ONLY closes the factory associated with getConfigurationFileName(). It does not commit pending transactions nor does it close any sessions.

Throws:
org.hibernate.HibernateException

destroyAllFactories

public static void destroyAllFactories()
                                throws org.hibernate.HibernateException
This method will close ALL SessionFactories that have been created. It does not commit pending transactions nor does it close any sessions.

Throws:
org.hibernate.HibernateException

addDaoListener

public void addDaoListener(DaoListener listener)
Adds a listener that will be notified when the dao has to roll back a transaction

Parameters:
listener -

removeDaoListeners

public void removeDaoListeners()
Removes all dao listeners from the listener list


fireRollbackEvent

protected void fireRollbackEvent()
Notifies all DaoListeners that have been registered that a Rollback Event has occured for this Dao.



Copyright © 2009. All Rights Reserved.