Skip to main content

When can I use hibernate as EJB3 persistence provider in GlassFish

Posted by ss141213 on January 17, 2006 at 1:08 AM PST

Can I use Hibernate in GlassFish? is a question I have been asked several times in recent past? Well, the answer is Yes & No(as of now), depending on what you are trying to use it as. By the way I must clarify, it's a NO only temporarily. Let's understand why:

If you want to use Hibernate as yet another persistence framework, there is already a nice and detailed posting about how to use hibernate in GlassFish. That article shows how to use Hibernate session APIs, hibernate O/R mapping XML files etc. It does not talk about using Hibernate as an EJB 3.0 persistence provider. That's the reason you find so many configuration steps mentioned in that article.

If you are trying to use Hibernate as an EJB 3.0 persistence provider, it is right now not possible. I must say, it's only a temporary no because of the mismatch in interfaces between GlassFish and Hibernate. When I say using Hibernate as an EJB 3.0 persistence provider, what I mean is that user is using EJB 3.0 persistence API(javax.persistence) classes. e.g. using @persistenceContext to inject an EntityManager, @Entity to annotate POJO classes as entity beans, persistence.xml to configure a persistence units etc. as demonstrated in in this example as opposed to using Hibernate session management APIs, Hibernate O/R mapping XML file to convert their POJO class to an entity bean etc. As you probably know the EJB 3.0 (JSR 220) spec defines a Service Provider Interface(SPI) to plug in a compliant persistence provider to a Java EE 5 container like GlassFish. These SPI classes are part of javax.persistence.spi package. If you look at the revision history of the EJB 3 Persistence(a.k.a. Java Persistence API) spec, you will see that (unfortunately) the SPI has been changing quite a lot while the spec is being finalized. So it is hard for any two different projects to keep in synch with each other. Now that the final draft of the EJB 3 spec has been proposed, hopefully(!) we have a stable interface that both GlassFish and Hibernate can soon implement to achieve this pluggability.

EJB 3.0 Persistence SPI
The SPI classes are part of javax.persistence.spi package. Users don't have to know about this package while using Java Persistence API inside a container. It is used by the container and persistence provider to provide the necessary pluggability. This is indeed good news for developers as they can use a combination of their favorite Java EE container with their favorite persistence provider without sacrificing portability of their applications. Some other day I shall talk more the SPI and issues around it.

Some EJB 3.0 Persistence API related resources:

Introduction to using Java Persistence API in Java EE
Latest builds of GlassFish where an open source implementation of this spec is being done.
GlassFish persistence project

More articles about

Related Topics >>