#StackBounty: #java #hibernate #jpa #gradle Unknown entity with Hibernate upgraded to 5.2.3 and above

Bounty: 50

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:

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

and my EntityBase class:

@XmlAccessorType(value = XmlAccessType.NONE)
@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.
    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"
    <persistence-unit name="PersistenceUnit">


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

            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="true"/>

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:

    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?

Get this bounty!!!