How to get the Hibernate Session from the JPA 2.0 EntityManager

This tip shows you how to get the Hibernate Session from the JPA EntityManager so you can perform functions only available from the JBoss Hibernate project.

With so many benefits and drawbacks to using either the Hibernate Session or the JPA EntityManager for performing object relational mapping, it is no wonder why enterprise architects have such a difficult time choosing between them. The JPA EntityManager is part of the Java Persistence API standard. However, the Hibernate Session provides many features that go above and beyond the JPA specification, with Criteria query capabilities being a compelling legacy feature that JPA 1.0 did not have, and Hibernate Envers being a compelling reason to use the Hibernate 4.0 Session object over its JPA brethren.

According to Emmanuel Bernard, a data architect for JBoss who is part of the Hibernate team, the preferred option for organizations adopting Hibernate to perform ORM tasks is to use the JPA EntityManager. So what should IT developers do if they need to get to the underlying Hibernate Session to access its extra features? Fortunately, the JPA specification makes it possible to access the underlying Hibernate Session, even if your application is using the JSR-317 standard.

JPA 1.0 access to the Hibernate Session

To obtain the Hibernate Session from a JPA 1.0 EntityManager, the following code would be required:

org.hibernate.Session hibernateSession = (Session)entityManager.getDelegate();

Unfortunately, this syntax isn't completely portable, as this code works fine on a JBoss Application Server, but will have unpredictable results when run on other platforms.

JPA 2.0 access to the Hibernate Session

Fortunately, JPA 2.0 provides a better, more portable and more cross-platform approach to obtaining the Hibernate Session. The code to do so is as follows:

Session session = entityManager.unwrap(org.hibernate.Session.class);

And that's it! That's how easy it is to obtain the underlying Hibernate implementation class when working with the JPA EntityManager. So make this code a part of your HibernateUtil class, and make it possible for the persistence store that is working with your entity beans to morph into a Hibernate Session, allowing you to take advantage of the many neat features that go above and beyond the standard JPA specification.

This was last published in June 2013

Dig Deeper on Java EE development and enterprise Java platforms

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

It's good that you can get the code for the hibernate session, but if you don't have the JBoss-application server what are the unpredictable results?
Cancel

-ADS BY GOOGLE

SearchCloudApplications

SearchSOA

SearchSoftwareQuality

SearchFinancialApplications

SearchSAP

SearchManufacturingERP

Close