eskatos's thoughts

Quick and dirty howto : Use Hibernate in Glassfish V2

with 15 comments


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:


You can also specify hibernate properties as usual, for example:

<properties>  <property name=”” 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.

  1. Download Hibernate projects
  2. Put’em in the domain’s lib dir
  3. Change your persistence.xml file
  4. Enjoy :)

Details are following :

1) Download Hibernate projects

Get your favorite browser to the Hibernate download page and get the following files :

  • Hibernate Core
  • 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 :

  1. <?xml version=“1.0” encoding=“UTF-8”?>

  2. <persistence version=“1.0” xmlns=;

  3. xmlns:xsi=;

  4. xsi:schemaLocation=;>

  5. <persistence-unit name=“MyEntitiesPU” transaction-type=“JTA”>

  6. <provider>org.hibernate.ejb.HibernatePersistence</provider>

  7. <jta-data-source>jdbc/__default</jta-data-source>

  8. <class></class>

  9. <exclude-unlisted-classes>true</exclude-unlisted-classes>

  10. <properties>

  11. <property name=“hibernate.dialect” value=“org.hibernate.dialect.DerbyDialect”/>

  12. <property name=“” value=“create-drop”/>

  13. </properties>

  14. </persistence-unit>

  15. </persistence>

4) Enjoy :)

Restart your glassfish and enjoy Hibernate !

Additions :

Written by Paul

October 9, 2007 at 9:48 am

Posted in Code, HOWTOs

Tagged with , , ,

15 Responses

Subscribe to comments with RSS.

  1. Nice blog eskatos !


    October 9, 2007 at 9:50 am

  2. Short and chweet

    Abubakar Gurnah

    October 10, 2007 at 9:47 am

  3. […] 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) […]

  4. 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 :




    October 12, 2007 at 11:07 am

  5. […] 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 […]

  6. Did you try to do a rollback on a transaction, and did you verify if records are properly removed from the DB?


    December 3, 2007 at 12:41 pm

  7. @olivier : yes and it works like a charm


    December 3, 2007 at 3:05 pm

  8. […] Quick and dirty howto : Use Hibernate in Glassfish V2 « Eskatos’s thoughts on JEE development (tags: hibernate) […]

  9. 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

  10. @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.


    April 23, 2009 at 12:19 am

  11. 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?

    public class Test {

    @PersistenceUnit(name = “citespace-jpa”)
    protected EntityManagerFactory emf;

    public String getIt() {

    EntityManager manager = emf.createEntityManager();
    Query q = manager.createQuery(“SELECT c from Country c where :id “);
    q.setParameter(“id”, 1);
    List c = q.getResultList();

    return “Hi there!”;

    Because I am getting NullPointerException. Can u help me please?


    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);


      October 9, 2009 at 4:48 pm

  12. Carl van Denzen

    June 8, 2010 at 4:38 pm

  13. 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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: