org.openxava.hibernate
Class XHibernate

java.lang.Object
  extended by org.openxava.hibernate.XHibernate

public class XHibernate
extends java.lang.Object

Allows to work easily with hibernate inside OpenXava applications.

You can use this class in any part of your OpenXava application: calculators, validators, actions, junit tests, etc.
To use hibernate with this class you can write something as:

 Invoice invoice = new Invoice();
 XHibernate.getSession().save(invoice);
 
And no more.
The method getSession() create a session and transaction the first time in the thread, and the OpenXava close the session at the end of action execution.
Also the next code is legal:
 Invoice invoice = new Invoice();
 XHibernate.getSession().save(invoice);
 XHibernate.commit(); // In this way you commit and close manually.
 ...
 XHibernate.getSession().save(customer); // As session has been closed, a new one is created
 
Per thread configuration
XHibernate allows you to change the configuration in runtime affecting only to the current thread of execution.
For example, if you want to have two configuration: 'hibernate1.cfg.xml' and 'hibernate2.cfg.xml', and you want to change from one to other, it's easy:
 XHibernate.setConfigurationFile("/hibernate1.cfg.xml");
 XHibernate.getSession().save(invoice); // Using hibernate1
 ...
 XHibernate.commit();
 XHibernate.setConfigurationFile("/hibernate2.cfg.xml");
 XHibernate.getSession().save(invoice); // Using hibernate2
 ...
 XHibernate.commit();
 
And this change only affect to the current execution thread.
Or if you want to change the default schema you can write:
 XHibernate.setDefaultSchema("COMPANY1");
 XHibernate.getSession().save(invoice); // Save in INVOICE table of COMPANY1 Schema
 ...
 XHibernate.commit();
 XHibernate.setDefaultSchema("COMPANY2");
 XHibernate.getSession().save(invoice); // Save in INVOICE table of COMPANY2 Schema
 ...
 XHibernate.commit();  
 
Also this only affect to the current thread.
The method setSessionFactoryProperties(java.util.Properties) can be use in the same way that setConfigurationFile(java.lang.String) and setDefaultSchema(java.lang.String).

Author:
Javier Paniza

Constructor Summary
XHibernate()
           
 
Method Summary
static void commit()
          Commits changes and closes the session associated to current thread.
static org.hibernate.Session createSession()
          Create a new session.
static java.lang.String getConfigurationFile()
          By default is /hibernate.cfg.xml.
static java.lang.String getDefaultSchema()
          The default schema used by Hibernate in the current thread.
static org.hibernate.Session getSession()
          Session associated to current thread.
static java.util.Properties getSessionFactoryProperties()
          The properties sent to Hibernate to in order to create the SessionFactory.
static boolean isCmt()
          Indicate that the current thread is executing inside a CMT context.
static void reset()
          Reset the info associated to the current thread.
static void rollback()
          Rollback changes and closes the session associated to current thread.
static void setCmt(boolean cmt)
          Indicate that the current thread is executing inside a CMT context.
static void setConfigurationFile(java.lang.String configurationFile)
          By default is /hibernate.cfg.xml.
static void setDefaultSchema(java.lang.String defaultSchema)
          Change the default schema used by Hibernate in the current thread.
static void setSessionFactoryProperties(java.util.Properties sessionFactoryProperties)
          Set the properties to send to Hibernate in order to create the SessionFactory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XHibernate

public XHibernate()
Method Detail

getSession

public static org.hibernate.Session getSession()
Session associated to current thread.

If no session exists or it's closed, then create a new one, and start a transaction.

Returns:
Not null

createSession

public static org.hibernate.Session createSession()
Create a new session.

This session is not associated with the current thread, and no transaction is started.

Returns:
Not null

commit

public static void commit()
Commits changes and closes the session associated to current thread.

If no session or the it is closed this method does nothing.
In most cases this method is called by OpenXava automatically, hence the programmer that uses hibernate APIs does not need to call it.


rollback

public static void rollback()
Rollback changes and closes the session associated to current thread.

If no session or the it is closed this method does nothing.


getConfigurationFile

public static java.lang.String getConfigurationFile()
By default is /hibernate.cfg.xml.

You must set value to this property before use any other method of this class. The value of this properties may be different for each thread.


setConfigurationFile

public static void setConfigurationFile(java.lang.String configurationFile)
By default is /hibernate.cfg.xml.

You must set value to this property before use any other method of this class. If you change this value it only take effect for the current thread.

If you sent a null then /hibernate.cfg.xml is assumed.


setCmt

public static void setCmt(boolean cmt)
Indicate that the current thread is executing inside a CMT context.

CMT is Container Managed Transaction. The usual inside EJB.


isCmt

public static boolean isCmt()
Indicate that the current thread is executing inside a CMT context.

CMT is Container Managed Transaction. The usual inside EJB.


getSessionFactoryProperties

public static java.util.Properties getSessionFactoryProperties()
The properties sent to Hibernate to in order to create the SessionFactory.

The value can be different for each execution thread.

Returns:
Not null

setSessionFactoryProperties

public static void setSessionFactoryProperties(java.util.Properties sessionFactoryProperties)
Set the properties to send to Hibernate in order to create the SessionFactory.

This only apply to the current execution thread.


getDefaultSchema

public static java.lang.String getDefaultSchema()
The default schema used by Hibernate in the current thread.

For example, if you use 'COMPANYA' as default schema, and you OX component is mapping to a table named 'ISSUE' when OX and JPA engine try to execute SQL they will use 'COMPANYA.ISSUE' as table name.


setDefaultSchema

public static void setDefaultSchema(java.lang.String defaultSchema)
Change the default schema used by Hibernate in the current thread.

For example, if you use 'COMPANYA' as default schema, and you OX component or EJB3 entity is mapping to a table named 'ISSUE' when OX and JPA engine try to execute SQL they will use 'COMPANYA.ISSUE' as table name.


reset

public static void reset()
Reset the info associated to the current thread.

After call this method XPersistence works as default, all previous call to #setPersistenceUnit, #setPersistenceUnitProperties or setDefaultSchema(java.lang.String) are annulled.