shared
Class HibernateHelper

java.lang.Object
  extended by shared.HibernateHelper

public class HibernateHelper
extends java.lang.Object

Helper methods for accessing Hibernate.


Field Summary
protected static java.lang.Exception lastError
          The last error that was generated by Hibernate.
protected static java.util.List<java.lang.Class> listClasses
          The list of classes that are known to Hibernate.
protected static org.apache.log4j.Logger log
          Logger for reporting errors.
protected static org.hibernate.SessionFactory sessionFactory
          The session factory that is used by Hibernate for all access.
 
Constructor Summary
HibernateHelper()
           
 
Method Summary
protected static boolean addMappings(java.util.List<java.lang.Class> list, java.lang.Class... mappings)
          Test if the class being added is already known by Hibernate.
protected static org.hibernate.SessionFactory buildFactory(org.hibernate.cfg.Configuration cfg)
          Build the factory.
static void closeFactory()
          Close the factory.
static void closeFactory(org.hibernate.SessionFactory factory)
          Close the factory.
protected static void configureFromFile(org.hibernate.cfg.Configuration cfg)
          Read from the hibernate.cfg.xml file.
protected static org.hibernate.SessionFactory createFactory(java.util.Properties props, java.util.List<java.lang.Class> list)
          Create the session factory.
static void createTable(java.lang.Class... mappings)
          Overloaded for no properties.
static void createTable(java.util.Properties props, java.lang.Class... mappings)
          Create a temporary session factory to create the tables.
static java.lang.Object getFirstMatch(java.lang.Class classBean, java.lang.String strKey, java.lang.Object value)
          Retreives the first record that has a field that matches the specified value.
static java.lang.Object getFirstMatch(java.lang.Object data, java.lang.String strKey, java.lang.Object value)
          Overload of getFirstMatch that accepts a bean object instead of a bean class.
static java.lang.Object getKeyData(java.lang.Class beanClass, long itemId)
          Retrieves the bean with the specified value for the key field
static java.lang.Exception getLastError()
          Retrieve the last error generated by Hibernate.
static java.util.List getListData(java.lang.Class classBean)
          Retrieve all the records from the database.
static java.util.List getListData(java.lang.Class classBean, java.lang.String strKey, java.lang.Object value)
          Use a criteria object to retrieve records from the database.
static void initSessionFactory(java.lang.Class... mappings)
          Overloaded initSessionFactory for not sending props.
static void initSessionFactory(java.util.Properties props, java.lang.Class... mappings)
          Initialise the session factory that Hibernate uses for all access.
static boolean isSessionOpen()
          Tests if the sessionFactory exists.
static void removeDB(java.lang.Object obj)
          Call the Hibernate method delete.
static void saveDB(java.lang.Object obj)
          Call the Hibernate method save.
protected static void someDatabaseProcess()
          A dummy method, used to test if the database is open.
protected static void testConnection(org.hibernate.SessionFactory factory)
          Test if the connection to the database works.
static boolean testDB(javax.servlet.http.HttpServletResponse response)
          Tests if a database operation will succeed on the sessionFactory.
static void updateDB(java.util.List list)
          Call the Hibernate method saveOrUpdate.
static void updateDB(java.lang.Object obj)
          Call the Hibernate method saveOrUpdate.
static void writeError(javax.servlet.http.HttpServletResponse response)
          Writes an error page for the last generated error
static void writeError(javax.servlet.http.HttpServletResponse response, java.lang.String title, java.lang.Exception ex)
          Writes an error message to the response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.log4j.Logger log
Logger for reporting errors.


listClasses

protected static java.util.List<java.lang.Class> listClasses
The list of classes that are known to Hibernate.


sessionFactory

protected static org.hibernate.SessionFactory sessionFactory
The session factory that is used by Hibernate for all access.


lastError

protected static java.lang.Exception lastError
The last error that was generated by Hibernate.

Constructor Detail

HibernateHelper

public HibernateHelper()
Method Detail

initSessionFactory

public static void initSessionFactory(java.util.Properties props,
                                      java.lang.Class... mappings)
Initialise the session factory that Hibernate uses for all access.

Parameters:
props - Properties for initalising Hibernate
mappings - The classes that are to be mapped by Hibernate

initSessionFactory

public static void initSessionFactory(java.lang.Class... mappings)
Overloaded initSessionFactory for not sending props.

Parameters:
mappings - The list of classes that are to be mapped.

createTable

public static void createTable(java.util.Properties props,
                               java.lang.Class... mappings)
Create a temporary session factory to create the tables.

Parameters:
props - The properties to initialise Hibernate
mappings - The classes to be mapped in Hibernate

createTable

public static void createTable(java.lang.Class... mappings)
Overloaded for no properties.

Parameters:
mappings - The classes to be mapped in Hibernate

addMappings

protected static boolean addMappings(java.util.List<java.lang.Class> list,
                                     java.lang.Class... mappings)
Test if the class being added is already known by Hibernate.

Parameters:
list - The list of known classes
mappings - The new mappings being added
Returns:
true if there is a new class in the mappings

createFactory

protected static org.hibernate.SessionFactory createFactory(java.util.Properties props,
                                                            java.util.List<java.lang.Class> list)
Create the session factory.

Parameters:
props - The properties to initialise Hibernate
list - The list of classes to be mapped in Hibernate
Returns:
the session factory

configureFromFile

protected static void configureFromFile(org.hibernate.cfg.Configuration cfg)
                                 throws java.lang.Exception
Read from the hibernate.cfg.xml file.

Parameters:
cfg - The configuration for initialising Hibernate
Throws:
java.lang.Exception

buildFactory

protected static org.hibernate.SessionFactory buildFactory(org.hibernate.cfg.Configuration cfg)
                                                    throws java.lang.Exception
Build the factory. Log errors.

Parameters:
cfg - The configuration for initialising Hibernate
Returns:
Throws:
java.lang.Exception

testConnection

protected static void testConnection(org.hibernate.SessionFactory factory)
                              throws java.lang.Exception
Test if the connection to the database works.

Parameters:
factory - The initialised session factory
Throws:
java.lang.Exception

someDatabaseProcess

protected static void someDatabaseProcess()
A dummy method, used to test if the database is open.


closeFactory

public static void closeFactory(org.hibernate.SessionFactory factory)
Close the factory.

Parameters:
factory - The initialise session factory

closeFactory

public static void closeFactory()
Close the factory.


getLastError

public static java.lang.Exception getLastError()
Retrieve the last error generated by Hibernate.

Returns:
last error

updateDB

public static void updateDB(java.lang.Object obj)
Call the Hibernate method saveOrUpdate. Hibernate uses transactions and sessions to access the database. If the object is already in the database, then an update will occur; otherwise, a save of a new record occurs.

Parameters:
obj - The object to save to the database

updateDB

public static void updateDB(java.util.List list)
Call the Hibernate method saveOrUpdate. Hibernate uses transactions and sessions to access the database. If the object is already in the database, then an update will occur; otherwise, a save of a new record occurs.

Parameters:
list - The list of obejcts to save or update

saveDB

public static void saveDB(java.lang.Object obj)
Call the Hibernate method save. Hibernate uses transactions and sessions to access the database. A new row will always be added to the database.

Parameters:
obj - The object to save to the database

removeDB

public static void removeDB(java.lang.Object obj)
Call the Hibernate method delete. Hibernate uses transactions and sessions to access the database. The associated row will be deleted from the database.

Parameters:
obj - The object to delete from the database

getListData

public static java.util.List getListData(java.lang.Class classBean,
                                         java.lang.String strKey,
                                         java.lang.Object value)
Use a criteria object to retrieve records from the database.

Hibernate uses transactions and sessions to access the database. Retrieves a list of beans whose field matches the given value.

Parameters:
classBean - The bean class
strKey - The field name
value - The value of the field
Returns:
The list of beans whose key matches the value

getListData

public static java.util.List getListData(java.lang.Class classBean)
Retrieve all the records from the database.

Hibernate uses transactions and sessions to access the database. Retrieves all the records from the database in a list.

Parameters:
classBean -
Returns:

getFirstMatch

public static java.lang.Object getFirstMatch(java.lang.Class classBean,
                                             java.lang.String strKey,
                                             java.lang.Object value)
Retreives the first record that has a field that matches the specified value.

Parameters:
classBean - The bean class for the database table
strKey - The field to search
value - The value to match
Returns:
The first bean that whose field matches the value

getFirstMatch

public static java.lang.Object getFirstMatch(java.lang.Object data,
                                             java.lang.String strKey,
                                             java.lang.Object value)
Overload of getFirstMatch that accepts a bean object instead of a bean class. Retreives the first record that has a field that matches the specified value.

Parameters:
data - A bean object that matches the bean class for the database
strKey - The field to search
value - The value to match
Returns:
The first bean that whose field matches the value

getKeyData

public static java.lang.Object getKeyData(java.lang.Class beanClass,
                                          long itemId)
Retrieves the bean with the specified value for the key field

Parameters:
beanClass - The class for the database
itemId - The id for the bean
Returns:
The bean whose key field matches the value

isSessionOpen

public static boolean isSessionOpen()
Tests if the sessionFactory exists.

Returns:
true/false if session is open/closed

testDB

public static boolean testDB(javax.servlet.http.HttpServletResponse response)
                      throws java.io.IOException,
                             javax.servlet.ServletException
Tests if a database operation will succeed on the sessionFactory.

Parameters:
response - Response information encapsulated in a Java object
Returns:
true if the sessionFactory performs the operation
Throws:
java.io.IOException
javax.servlet.ServletException

writeError

public static void writeError(javax.servlet.http.HttpServletResponse response,
                              java.lang.String title,
                              java.lang.Exception ex)
                       throws java.io.IOException,
                              javax.servlet.ServletException
Writes an error message to the response.

Parameters:
response - Response information encapsulated in a Java object
title - Title for the page
ex - Exception that caused the error
Throws:
java.io.IOException
javax.servlet.ServletException

writeError

public static void writeError(javax.servlet.http.HttpServletResponse response)
                       throws java.io.IOException,
                              javax.servlet.ServletException
Writes an error page for the last generated error

Parameters:
response - Response information encapsulated in a Java object
Throws:
java.io.IOException
javax.servlet.ServletException