2017-02-27 83 views
10

,搖籃無法再找到我的實體類:與Hibernate未知實體只要我升級到5.2.3休眠或以上升級到5.2.3及以上

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) 

它不發生在Hibernate 5.2.2及更低版本中。

這並沒有解決它:what is gradle missing to map hibernate?

這是我的用戶等級:

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

和我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(); 
    } 
} 

測試有自己的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> 

運行測試時,我也會得到這個異常:

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

這很奇怪,因爲這隻發生在運行Gradle時發生,沒有它,一切都是罰款

更新1

顯然,這個問題是不相關的搖籃可言,因爲我剛剛意識到的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 

這讓我實現問題是,由於Hibernate 5.2.3使用第二個persistence.xml,只是爲了單元測試不工作

所以我有一個src/main/resources/META-INF/persistence.xml 和src/test/resources/META-INF/persistence.xml,但它始終從src/main/...讀取persistence.xml,從版本5.2開始0.3 :(

更新2

好吧,我知道如何重現該問題,我只是不知道爲什麼它的發生或如何解決它。

我有一個使用最新Gradle版本的多模塊設置。我有一個包含休眠依賴性的build.gradle核心模塊:

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

再有就是我的模塊:其中單元測試失敗:

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

    testCompile project(":core").sourceSets.test.output 
} 

如果我註釋掉最後一行( 「testCompile項目(」:core「)。sourceSets.test.output」)它工作正常。

這是爲什麼?

+0

如果你發佈了你的'User'類和它的註釋以及你如何註冊這些類被Hibernate拾起,無論是persistence.xml還是一些彈簧配置等。 – Naros

+0

完成,謝謝。 – CryNickSystems

+0

我假設你只是創建一個'User'實例,設置值,然後調用'session.save(userInstance)'? – Naros

回答

0

嘗試上傳到Hibernate 5.2.9。最後,以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; 
    } 
} 
0

我面臨同樣的情況,但不是同樣的錯誤。測試資源(類,conf文件等)不應該存儲在core的測試部分中,而應存儲在獨立項目中。

我的解決辦法:

  1. 創建一個單獨的core-test項目
  2. 插入到其中(src/main)存儲在core
  3. 在你的模塊src/test部分的可重用的代碼,通過testCompile project(":core-test")
  4. 更換 testCompile project(":core").sourceSets.test.output

它應該可以解決您的問題。

相關問題