1

I'm made a small test project using Java EE 7 and Hibernate 4.2.2 and Glassfish 4.

Here are my pom.xml (web)

 <dependencies>        
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>br.com.myproject</groupId>
        <artifactId>escola-ejb</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

ejb

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>
<!--Hibernate-->     
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.2.Final</version>
    <scope>compile</scope>
</dependency>

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.2.Final</version>
        <scope>compile</scope>
</dependency>

It has only one entity called Student. And some crud page with list, create and edit. I can compile and deploy but when I try to open the list list page (all pages where generated by netbeans 7.3.1 with the option generate JSF pages from entity models), I got an error:

A system exception occurred during an invocation on EJB StudentFacade, method: public java.util.List br.com.myproject.ejb.facade.AbstractFacade.findRange(int[]).
at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748)
    at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:698)
    at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4475)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2009)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1979)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)

(...)

If I change the persistence.xml to to Eclipse link it works fine. But I rollback to hibernate. I got this error.

I've googled a lot but I'm not able to find any information as the Java EE 7 is a new technology.

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82

2 Answers2

2

Hibernate 4.2.2 is compatible with JPA 2.0. Glassfish 4 and EE 7 comes with JPA 2.1. For that you need Hibernate 4.3.0.Beta1 or later.

It might work with earlier versions of Hibernate but you will most likely run into problems. I would recommend to wait until they release a 4.3 final version of Hibernate.

  • I've tried with 4.3.0.Beta5 and no success. I was able to deploy but I was not able to get any data from the database. – Jonathas Pacífico Oct 17 '13 at 12:14
  • 1
    Had the same issue then asked my former Java teacher's help. He told me Hibernate 4.3.0.Beta5 doesn't have all implementations needed for building a completely working application, so I had to migrate to the latest EclipseLink to continue using GlassFish 4 and JEE7. Done that and discovered EclipseLink is much faster than Hibernate, plus it's the reference implementation. – Rasshu Nov 07 '13 at 15:34
  • 1
    There's Hibernate 4.3.1 Final now. But didn't give it a try already. – Rasshu Feb 17 '14 at 11:51
1

Simply adding Hibernate as a dependency to your application won't work. You either have to add the Hibernate JARs to GlassFish's server classpath (in which case Hibernate would be available as a JPA provider for all applications), or you bundle the Hibernate JARs in the lib directory of an EAR (in which case only that application would use Hibernate). See this blog for an example of the later case: http://javafromthetrenches.wordpress.com/2011/01/15/using-hibernate-jpa-with-glassfish/

You don't need to do anything special to use JTA and an EclipseLink (or Hibernate) datasource. Session beans are automatically transactional, so inject an entity manager instance into your session bean and use it from the business methods.

I should also point out that you'll need to add JDBC resources to GlassFish if you're not using the default Java DB database.

Ian Evans
  • 1,357
  • 10
  • 10
  • On the contrary, it's exactly the point. You need to ensure that your application and GlassFish are configured correctly if you want to use an alternate JPA layer. I say this because you state that EclipseLink works but Hibernate doesn't. If you dig into GlassFish's server.log file you'll probably see the root cause of the exception thrown when StudentFacade is called on the List view. – Ian Evans May 20 '14 at 21:09
  • as stated by many others users, the problem here is the hibernate version that is not compatible. – Jonathas Pacífico May 23 '14 at 19:38
  • And is your EAR correctly configured to bundle the Java EE 7 compatible version of Hibernate, using the maven-ear-plugin as described in the blog post above? – Ian Evans Jun 01 '14 at 00:22