Quick and dirty howto : Use Hibernate in Glassfish V2
UPDATE:
As Leon pointed in comments, it’s not a good idea to put the hibernate jars in the app server classpath like in this post. Hibernate community had published a howto explaining how to use Hibernate in Glassfish but the link has expired. Here is a copy from google cache:
To use hibernate with glassfish, all you have to do is put hibernate and its dependencies into your EAR file. This might also with with a WAR file.
In your persistence.xml, specify hibernate using this tag:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
You can also specify hibernate properties as usual, for example:
<properties> <property name=”hibernate.hbm2ddl.auto” value=”none”/></properties></blockquote>

For my first post here I’ll write a quick and dirty howto for using Hibernate inside the Glassfish V2 container.
- Download Hibernate projects
- Put’em in the domain’s lib dir
- Change your persistence.xml file
- Enjoy :)
Details are following :
1) Download Hibernate projects
Get your favorite browser to the Hibernate download page and get the following files :
- Hibernate Core 3.2.5.ga
- Hibernate Annotations 3.3.0 GA
- Hibernate EntityManager 3.3.1 GA
- Hibernate Validator 3.0.0 GA
- Hibernate Search 3.0.0 GA
Theses versions were the last stable releases versions when this blog was written, addapt them to your own era.
Unpack them all.
2) Put the right jars in the domain’s lib dir
Take the following JARs :
- hibernate3.jar
- hibernate-annotations.jar
- hibernate-commons-annotations.jar
- hibernate-entitymanager.jar
- hibernate-search.jar
- hibernate-validator.jar
- antlr-2.7.6.jar
- asm-attrs.jar
- asm.jar
- c3p0-0.9.1.jar
- cglib-2.1.3.jar
- commons-collections-2.1.1.jar
- commons-logging-1.0.4.jar
- concurrent-1.3.2.jar
- dom4j-1.6.1.jar
- ehcache-1.2.3.jar
- javassist.jar
- jboss-archive-browsing.jar
- log4j-1.2.11.jar
- lucene-core-2.2.0.jar
… and put them all in the “lib/” subdirectory of your Glassfish domain.
3) Change your persistence.xml file
Change your persistence.xml file to build a persistence unit that use Hibernate persistence mechanism :
Here is an example :
-
<?xml version=“1.0″ encoding=“UTF-8″?>
-
<persistence version=“1.0″ xmlns=“http://java.sun.com/xml/ns/persistence”
-
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
-
xsi:schemaLocation=“http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd”>
-
<persistence-unit name=“MyEntitiesPU” transaction-type=“JTA”>
-
<provider>org.hibernate.ejb.HibernatePersistence</provider>
-
<jta-data-source>jdbc/__default</jta-data-source>
-
<class>my.app.model.entities.Device</class>
-
<exclude-unlisted-classes>true</exclude-unlisted-classes>
-
<properties>
-
<property name=“hibernate.dialect” value=“org.hibernate.dialect.DerbyDialect”/>
-
<property name=“hibernate.hbm2ddl.auto” value=“create-drop”/>
-
</properties>
-
</persistence-unit>
-
</persistence>
4) Enjoy :)
Restart your glassfish and enjoy Hibernate !
Additions :

Nice blog eskatos !
Rabbit
October 9, 2007 at 9:50 am
Short and chweet
Abubakar Gurnah
October 10, 2007 at 9:47 am
[...] Use Hibernate in Glassfish V2 For my first post here I’ll write a quick and dirty howto for using Hibernate inside the Glassfish V2 container. (tags: hibernate) [...]
links for 2007-10-10 | The Third Part
October 10, 2007 at 10:25 am
One more little thing.
If you want to get the Hibernate logs you’ll have to configure log4j in Glassfish.
There’s a howto on the glassfish site at the following URL : http://wiki.glassfish.java.net/Wiki.jsp?page=FaqCongifureLog4J
Regards
eskatos
eskatos
October 12, 2007 at 11:07 am
[...] and WebServices – WSGEN Classpath Posted on October 16, 2007 by eskatos Following my quick and dirty how-to about using hibernate in glassfish I came across a small pitfall when dealing with metro [...]
Glassfish, Hibernate and WebServices - WSGEN Classpath « Eskatos’s thoughts on JEE development
October 16, 2007 at 1:39 am
Did you try to do a rollback on a transaction, and did you verify if records are properly removed from the DB?
olivier
December 3, 2007 at 12:41 pm
@olivier : yes and it works like a charm
eskatos
December 3, 2007 at 3:05 pm
[...] Quick and dirty howto : Use Hibernate in Glassfish V2 « Eskatos’s thoughts on JEE development (tags: hibernate) [...]
links for 2008-11-19 at AntonioScatoloni.it
November 19, 2008 at 11:01 am
I would recommend NOT putting hibernate into the domain lib dir, as this will require log4j to be present in the same library, since hibernate uses log4j for logging.
We want log4j, commons-logging to be loaded by the application classloader, so the configurations thereof apply to the application. IMHO, best is to deploy hibernate in the ejb-jar, and put log4j and commons-logging at the ear level. Everything works nicely in that set up.
Leon Gent
April 22, 2009 at 6:27 pm
@Leon: Thanks for bumping this, you’re right and this might confuse readers. This post is old and contains at the top a link to the official hibernate tutorial. The font might not be strong enough, I’ll fix that.
eskatos
April 23, 2009 at 12:19 am
I have a question, I would like to know how to integrate Jersey and JPA/Hibernate and how to use the EntityManager on Jersey.
Can I do it?
@Path(“/test”)
public class Test {
@PersistenceUnit(name = “citespace-jpa”)
protected EntityManagerFactory emf;
@GET
@Produces(“text/plain”)
public String getIt() {
EntityManager manager = emf.createEntityManager();
Query q = manager.createQuery(“SELECT c from Country c where c.id= :id “);
q.setParameter(“id”, 1);
List c = q.getResultList();
return “Hi there!”;
}
Because I am getting NullPointerException. Can u help me please?
Roan
October 9, 2009 at 4:06 pm
@Roan: First, be carrefull that this post is outdated as stated in its header.
Next, I don’t know where appends your NPE, I don’t know much about your persistence unit etc… so I cannot really help you on this.
You’d better post in the jersey or hibernate forums/mailing lists. You’ll get far more audience there.
BTW you should read some material about JPA. For example to retrieve a Country by ID you can write :
Country country = em.find(Country.class, id);
eskatos
October 9, 2009 at 4:48 pm
The link (at the start of this blog) to the explanation by the Hibernate community is broken (http://go2.wordpress.com/?id=725X1342&site=eskatos.wordpress.com&url=http%3A%2F%2Fwww.hibernate.org%2F374.html&sref=http%3A%2F%2Feskatos.wordpress.com%2F2007%2F10%2F09%2Fhello-world%2F)
Carl van Denzen
June 8, 2010 at 4:38 pm
Thanks for solution, that works in Glassfish now.
But I have one problem.
I hardcode jndi-source in persistence.xml so how can I write unit-test now ?
In unit test I want to create simple datasource (without jndi) and pass it to EntityManager.
Boris Romashov (@boraldomaster)
August 11, 2011 at 12:47 pm
See my posts about unit testing with in-memory databases http://eskatos.wordpress.com/2009/10/26/unit-test-jpa-entities-with-in-memory-derby/
You could put two distinct persistence units inside your persistence.xml file, one for production, the other one for tests.
Or, if you use maven (or another build tool you know) you could have one persistence.xml file for production, another one for tests.
eskatos
August 16, 2011 at 9:28 am