|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.nakedobjects.plugins.hibernate.objectstore.util.HibernateUtil
public class HibernateUtil
Basic Hibernate helper class for Hibernate configuration and startup.
Uses a static initializer to read startup options and initialize Configuration and SessionFactory.
This class also tries to figure out if JNDI binding of the SessionFactory is used, otherwise it falls back to a global static variable (Singleton). If you use this helper class to obtain a SessionFactory in your code, you are shielded from these deployment differences.
Another advantage of this class is access to the Configuration object that was used to build the current SessionFactory. You can access mapping metadata programmatically with this API, and even change it and rebuild the SessionFactory.
If you want to assign a global interceptor, set its fully qualified class name with the system (or hibernate.properties/hibernate.cfg.xml) property hibernate.util.interceptor_class. It will be loaded and instantiated on static initialization of HibernateUtil; it has to have a no-argument constructor. You can call HibernateUtil.getInterceptor() if you need to provide settings before using the interceptor.
Note: This class supports annotations by default, hence needs JDK 5.0 and the Hibernate Annotations library on the classpath. Change the single commented line in the source to make it compile and run on older JDKs with XML mapping files only.
Note: This class supports only one data store. Support for several SessionFactory instances can be easily added (through a static Map, for example). You could then lookup a SessionFactory by its name.
| Field Summary | |
|---|---|
static java.lang.String |
MAPPING_DIR
|
| Constructor Summary | |
|---|---|
HibernateUtil()
|
|
| Method Summary | |
|---|---|
static void |
commitTransaction()
Commits the transaction on the current session if one is active. |
static void |
ensureMapped(NakedObjectSpecification specification)
|
static org.hibernate.cfg.Configuration |
getConfiguration()
Returns the original Hibernate configuration. |
static org.hibernate.Session |
getCurrentSession()
Returns the current Session, and starts a new transaction if one is not active |
static java.lang.String |
getRequiredClasses()
|
static org.hibernate.SessionFactory |
getSessionFactory()
Returns the global SessionFactory. |
static boolean |
hasInitRun()
|
static boolean |
init()
|
static void |
initialiseSessionFactory()
|
static boolean |
inTransaction()
|
static boolean |
isDatabaseKeyword(java.lang.String potentialKeyword)
|
static void |
rebuildSessionFactory()
Rebuild the SessionFactory with the given Hibernate Configuration. |
static void |
rollbackTransaction()
Rolls back the transaction on the current session if one is active. |
static void |
shutdown()
Closes the current SessionFactory and releases all resources except the configuration. |
static void |
startTransaction()
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final java.lang.String MAPPING_DIR
| Constructor Detail |
|---|
public HibernateUtil()
| Method Detail |
|---|
public static void initialiseSessionFactory()
public static org.hibernate.cfg.Configuration getConfiguration()
public static org.hibernate.SessionFactory getSessionFactory()
public static org.hibernate.Session getCurrentSession()
public static void startTransaction()
public static boolean inTransaction()
public static void commitTransaction()
public static void rollbackTransaction()
public static void rebuildSessionFactory()
HibernateUtil does not configure() the given Configuration object, it directly calls buildSessionFactory(). This method also closes the old SessionFactory before, if still open.
cfg - public static java.lang.String getRequiredClasses()
public static boolean hasInitRun()
public static boolean init()
public static void shutdown()
public static boolean isDatabaseKeyword(java.lang.String potentialKeyword)
public static void ensureMapped(NakedObjectSpecification specification)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||