0

I'm trying to migrate from EclipseLink to Hibernate in my Java EE 7 application and running into some issues I'm not able to figure out.

Here my environment:

  • Glassfish 4.1
  • Hibernate 4.3.6
  • Java 1.8.0_20

I'm also using:

  • EE 7
  • CDI
  • JPA 2.1

Ultimately our goal is to be able to use Java 8 Streams and Lambdas, as our app has a lot of set manipulation, and EclipseLink currently doesn't support Streams. We have another project running with Hibernate (on Wildfly) that does this, but was hoping we could just migrate to Hibernate instead of swapping the whole Java EE App Server Stack.

So far here's what I've done, but mostly followed the guidance here: https://coderwall.com/p/e5fxrw

  1. I've installed the required and entity-manager Hibernate jars into my /glassfish/lib directory.

  2. I've modified my persistence-unit in persistence.xml as follows:

    <persistence-unit name="mypu" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>jdbc/myds</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.transaction.jta.platform" value="org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform" />
    <property name="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
    </persistence-unit>
    

Now the first exception I get is is "java.lang.IllegalArgumentException: Not an entity: ..." However my classes are all annotated with @Entity, and were working under EclipseLink. I don't think I should have to list my classes with the exclude-unlisted-classes = false and really would like to avoid that.

edit: Per unwichtich's suggestion, I've tried adding:

    <property name="hibernate.archive.autodetection" value="class, hbm"/>

and individually specifying the classes in the PU within the persistence.xml with no improvement.

Thanks for any assistance.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Jim
  • 668
  • 8
  • 22

2 Answers2

1

The problem seems to be a bug in the version of Hibernate 4.3.6+. I had the same problem and (after breaking head a whole day), I tried to change the version of Hibernate (was using 4.3.7). After some testing I noticed that the 4.3.5 version runs smoothly. Just as you presented using EclipseLink also not had any problems.

I tried to analyze the Hibernate source code, but I could not find the problem. Anyway, even if it is not a permanent solution, can be a workaround. In my case, I changed the project to use Hibernate 4.3.5.

I hope you enjoy.

Marcelo Barros
  • 930
  • 9
  • 16
0

You are right, this should work with your setup.

However, it may be that somehow Hibernate isn't able to discover your entities.

You can explicitly enable entity auto-discovery for Hibernate with this property in persistence.xml:

<property name="hibernate.archive.autodetection" value="class, hbm"/>

This should also be the default, even if you don't declare it.

If your entity classes are in a separate jar file you have to declare it like this:

<property name="hibernate.archive.autodetection" value="class, hbm, jar"/>

If nothing helps you may declare your classes manually like this:

<class>com.model.Entity</class>

Don't forget to Clean & Build your project before redeploying it.

See also:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66
  • Tried all this, including declaring the classes. No luck, however I see a 2 different exceptions now, 1) the IllegalArugmentException that I had before, and 2) a slightly different variation "java.lang.IllegalStateException: EntityManagerFactory is closed". I'm beginning to wonder if there's a bug/incompatibility between GF 4.1 and Hibernate 4.3.6. – Jim Oct 31 '14 at 03:34
  • Also I should note I get error #1 when starting the server and deploying, and error #2 when just trying to redeploy. – Jim Oct 31 '14 at 03:39
  • Also - I didn't mention.. I'm also using CDI, which might be throwing a wrench in this as well. – Jim Oct 31 '14 at 03:41