Hibernate的未知实体升级到5.2.3及更高版本

Joh*_*ese 10 java hibernate jpa gradle

一旦我升级到Hibernate 5.2.3或更高版本,Gradle就再也找不到我的实体类了:

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)
Run Code Online (Sandbox Code Playgroud)

Hibernate 5.2.2及更低版本不会发生这种情况.

这并没有解决这个问题:什么是gradle缺少映射休眠?.

这是我的用户类:

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

和我的EntityBase类:

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

测试有自己的persistence.xml,它看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

运行测试时我也会遇到此异常:

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

这很奇怪,因为只有在通过Gradle运行时才会发生,没有它一切都很好.

更新1

显然问题根本与Gradle无关,因为我刚刚意识到在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
Run Code Online (Sandbox Code Playgroud)

这让我意识到问题是既然Hibernate 5.2.3使用了第二个persistence.xml,只是单元测试不起作用

所以我有一个src /主/资源/ META-INF/persistence.xml中和一个src /测试/资源/ META-INF/persistence.xml中,但它一直从SRC阅读的persistence.xml /主/ ...开始版本5.2.3 :(

更新2

好的,我知道如何重现问题,我只是不知道它为什么会发生或如何解决它.

我使用最新的Gradle版本进行多模块设置.我有一个带有build.gradle的核心模块,它包含hibernate依赖项:

compile "org.hibernate:hibernate-core:5.2.3.Final"
Run Code Online (Sandbox Code Playgroud)

然后是我的模块,其中单元测试失败:

dependencies {
    // core functionalities
    compile project(":core")
    testCompile project(":core")

    testCompile project(":core").sourceSets.test.output
}
Run Code Online (Sandbox Code Playgroud)

如果我注释掉最后一行("testCompile project(":core").sourceSets.test.output")它正常工作.

这是为什么?

Dam*_*ero 0

尝试上传到 Hibernate 5.2.9.Final,例如使用 maven,并遵循以下持久性示例:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)