openxava / documentation / Hibernate

OpenXava supports Hibernate since the version 2.0.

Configure your application to work with Hibernate

Since v3.0 JPA, not Hibernate, is the default persistence engine, so if you are using v3.0 or better and you want to use Hibernate as persistence manager then edit the file properties/xava.properties of your project, and left it in this way:
# Hibernate
persistenceProviderClass=org.openxava.model.impl.HibernatePersistenceProvider
mapFacadeAsEJB=false
From now on, your application will use Hibernate instead of EJB3 JPA to manage object persistence.

Using Hibernate in your code

You can use Hibernate APIs in any part of an OpenXava application, that is, inside calculators, validators, actions, filters, etc.
In order to facilitate the use of Hibernate OpenXava provides the XHibernate class. For example, if you wish to store an object in a database using the Hibernate API, the normal way would be:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Customer customer = ... ;
session.save(customer);
session.getTransaction().commit();
session.close();
 
But, inside OpenXava and using XHibernate class you can write this:
Customer customer = ... ;
XHibernate.getSession().save(customer);
No more.
The first time that you call to XHibernate.getSession() a session is created and assigned to the current thread and a transaction is created too; the next time that you call it, the same Hibernate session is used. At the end of the complete cycle of action execution, OpenXava commits automatically the transaction and closes the session. Moreover, XHibernate.getSession() works well inside and outside of a CMT environment.
You can optionally commit the transaction in any moment calling to XHibernate.commit(), if after this you use XHibernate.getSession() an new session and a new transaction are created for you.
You can learn more seeing the API doc of org.openxava.hibernate.XHibernate class.