2011-12-02 57 views
7

我試圖將我的DepartmentMandator類保留到hsqhldb,但它會產生此錯誤。jpa錯誤使用非實體[class ch.printsoft.mailhouse.usermgr.entity.Department]作爲關係屬性中的目標實體

Exception Description: [class ch.printsoft.mailhouse.usermgr.entity.Mandator] uses a non-entity [class ch.printsoft.mailhouse.usermgr.entity.Department] as target entity in the relationship attribute [field departments]. 
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126) 
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:115) 
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) 
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) 

這些是我嘗試堅持我的數據庫的類。我真的不知道問題是什麼。

@Entity 
public class Mandator { 
    @Id 
    @GeneratedValue 
    private Integer id; 
    private String mandatorId; 
    @OneToMany(mappedBy = "mandator") 
    private List<MandatorUser> mandatorUsers; 
    @OneToMany(mappedBy = "mandator") 
    private List<SLAFamilyGroup> slaFamilyGroups; 
    @OneToMany(mappedBy = "mandator") 
    private List<Group> groups; 
    @OneToMany(mappedBy = "mandator") 
    private List<Department> departments; 
    @OneToMany(mappedBy = "mandator") 
    private List<CostUnit> costUnits; 



@Entity 
    public class Department { 
    @Id 
    @GeneratedValue 
    private Integer id; 
    private String name; 
    private String responsiblePerson; 
    private String location; 

    @ManyToOne(optional = false) 
    private Mandator mandator; 
    @ManyToMany 
    private List<DocumentUser> documentUsers; 

我真的嘗試過所有的東西,但沒有奏效。

回答

1

從@OneToMany註解從委託人類刪除的mappedBy

檢查所有相關的類被映射爲@Entity。 Department類與DocumentUser具有@ManyToMany關係。提供DocumentUser類的列表

+0

ok ..會嘗試它非常多 –

+0

異常說明:[class ch.printsoft.mailhouse.usermgr.entity.Mandator]使用非實體[class ch.printsoft.mailhouse.usermgr.entity.Department] as關係屬性中的目標實體[字段部門]。 –

+0

從Department部門刪除@ManyToOne批註上的@ManyToOne批註中的「optional = false」 – WeMakeSoftware

31

確保在persistence.xml中列出了兩個類,並且這兩個類都位於類路徑中。

請包含您的persistence.xml。

+0

謝謝。我轉移了一些東西,忘記檢查我的persistence.xml是否已更新。檢查它,即使你認爲它是正確的! :-) – woot

0

確保兩個「class」文件在同一個jar中。 確保您沒有兩個同名的.class文件!

0

確保您有兩個類具有PK屬性和無參數構造函數。 我在我的實體類中添加下面的代碼。

@Entity 
public class EntityClass{ 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    public int id; 
    public EntityClass() { 
    } 
    ... 
} 
0

檢查你mappedBy=xxx並確保xxx是整個項目唯一的。

18

我只是花了很多時間調試一個看似無法解釋的例外情況,所以我只是把我的故事留在這裏。

如果您使用的是JPA(例如EclipseLink 2.5.x)的某種舊版本實現,並且您的代碼庫中正在使用現代Java 8功能(例如lambda表達式) - 請勿使用在JPA實體類中。

EclipseLink 2.5的類元數據解析器在字節碼中遇到Java 8功能(如lambda表達式,方法引用等)時崩潰。內部崩潰是一個非常不友好的ArrayIndexOutOfBoundsException,但你甚至沒有看到它,因爲編織器代碼默默地忽略了元數據解析器崩潰,並且僅僅假設類沒有有趣的元數據。這導致它相信所討論的類不是一個實體,即使它具有所有適當的註釋,最終的結果是我們正在討論的錯誤信息。

底線:不要在JPA實體類中使用Java 8 lambda表達式。 EVER。

+1

測試並運行到完全相同的概率! Thx for BOTTOM-line修正了這個問題: -/ – LeO

+1

但是EclipseLink 2.6.4已經解決了這個問題,現在JPA與lambda一起工作很好!至少我測試過的東西;-) – LeO

+0

謝謝kFYatek!在我的情況下,一個實體被忽略沒有任何來自JPA的警告(在FINE日誌!!),然後在@OneToMany指向這個被忽略的實體,JPA拋出一個異常「實體A使用非實體B(被忽略的)... 如何在黑暗的雲中丟失1小時........:p 一個小的lambda表達式在我的實體B中丟失:)更換後,一切都OK! – Elloco

1

我希望這可以幫助別人。

我有同樣的問題。 前一天晚上,一切都很好。 在NetBeans中,我使用History檢查了錯誤中涉及的所有文件。 包含Persistence.xml。 什麼都沒有改變。

我也檢查過手動。 我已經嘗試了此線程中建議的所有解決方案,甚至遷移到eclipselink 2.6.4。 我已經刪除了persistence.xml中涉及的2個類,保存並再次添加。 錯誤仍然存​​在。

然後,我刪除了包含在persistence.xml中的實體類列表中的所有類,然後再次包含它們。

現在,它的工作。 魔法。

但是確實在歷史上沒有差異。 即使是單個字符。