Wednesday, April 12, 2017

Unknown entity with Hibernate upgraded to 5.2.3 and above

Leave a Comment

As soon as I upgrade to Hibernate 5.2.3 or above, Gradle isn't able to find my Entity Classes anymore:

Caused by: java.lang.IllegalArgumentException: Unknown entity: data.model.User     at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:768)     at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:744)     at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:749)     at core.data.model.service.PersistenceService.lambda$create$0(PersistenceService.java:105)     at core.data.model.service.PersistenceService.withTransaction(PersistenceService.java:156) 

It doesn't happen with Hibernate 5.2.2 and below.

This didn't fix it: what is gradle missing to map hibernate?.

This is my User class:

@Entity @XmlRootElement @Table(name = "user") public class User extends EntityBase {     // some attributes } 

and my EntityBase class:

@XmlRootElement @XmlAccessorType(value = XmlAccessType.NONE) @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class") public abstract class EntityBase {     /**      * The entity's UUID.      */     @Id     @XmlAttribute     private String uuid;      public EntityBase() {         uuid = UUID.randomUUID().toString();     } } 

The tests have their own persistence.xml, it looks like this:

<persistence 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_2_0.xsd"              version="2.0">     <persistence-unit name="PersistenceUnit">         <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>          <class>data.model.User</class>          <properties>             <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>             <property name="hibernate.connection.url" value="jdbc:h2:mem:exerciseHandlerTest;DB_CLOSE_DELAY=-1"/>              <property name="hibernate.hbm2ddl.auto" value="create-drop"/>              <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>              <property name="hibernate.transaction.jta.platform"                       value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>              <property name="hibernate.show_sql" value="false"/>             <property name="hibernate.format_sql" value="true"/>         </properties>     </persistence-unit> </persistence> 

I also get this Exception when running the tests:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Difficulty is not mapped [select count(difficulty) from Difficulty difficulty] 

It is weird since this is happening only when running through Gradle, without it everything is fine.

Update 1

Apparently the problem isn't related to Gradle at all, because I just realized I am getting these Exceptions when running my Tests in IntellIJ:

java.lang.ExceptionInInitializerError     at core.test.BaseTest.<init>(NablaTest.java:55) Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]     ... Caused by: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [java:comp/env/jdbc/foobar]     ... 37 more Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial     ... 47 more 

This makes me realize the Problem is that Since Hibernate 5.2.3 using a second persistence.xml, just for the Unit Tests isn't working

So I have a src/main/resources/META-INF/persistence.xml and a src/test/resources/META-INF/persistence.xml, but it keeps reading the persistence.xml from src/main/... starting with version 5.2.3 :(

Update 2

Okay, I know how to reproduce the problem, I just don't know why it's happening or how to fix it.

I have a multi-module-setup using the latest Gradle version. I have a core module with a build.gradle that contains the hibernate dependency:

compile "org.hibernate:hibernate-core:5.2.3.Final" 

And then there's my module where the unit tests are failing:

dependencies {     // core functionalities     compile project(":core")     testCompile project(":core")      testCompile project(":core").sourceSets.test.output } 

If I comment out the last line ("testCompile project(":core").sourceSets.test.output") it's working fine.

Why is that?

2 Answers

Answers 1

try to upload to Hibernate 5.2.9.Final, using maven for example, and follow this persistence example:

@Entity public class Customer {      @Id     private Integer id;      private String name;      @Basic( fetch = FetchType.LAZY )     private UUID accountsPayableXrefId;      @Lob     @Basic( fetch = FetchType.LAZY )     @LazyGroup( "lobs" )     private Blob image;      public Integer getId() {         return id;     }      public void setId(Integer id) {         this.id = id;     }      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      public UUID getAccountsPayableXrefId() {         return accountsPayableXrefId;     }      public void setAccountsPayableXrefId(UUID accountsPayableXrefId) {         this.accountsPayableXrefId = accountsPayableXrefId;     }      public Blob getImage() {         return image;     }      public void setImage(Blob image) {         this.image = image;     } } 

Answers 2

I faced the same type of situation, not the same error though. Test resources (class, conf files, etc...) should not be stored in the test part of core but in a standalone project.

My solution :

  1. Create a separate core-test project
  2. Insert into it (in src/main) the reusable code stored in the src/test part of core
  3. In your modules, replace testCompile project(":core").sourceSets.test.output by testCompile project(":core-test")

It should resolve your issue.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment