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>

Source

 

 

Hibernate


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

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

  2. <persistence version=“1.0” xmlns=http://java.sun.com/xml/ns/persistence&#8221;

  3. xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

  4. xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&#8221;>

  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>my.app.model.entities.Device</class>

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

  10. <properties>

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

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

  13. </properties>

  14. </persistence-unit>

  15. </persistence>

4) Enjoy :)

Restart your glassfish and enjoy Hibernate !

Additions :

15 thoughts on “Quick and dirty howto : Use Hibernate in Glassfish V2

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

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

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

    1. @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);

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

Leave a comment