2012-12-12 85 views
1

我有兩個類這樣JPA與@ManyToOne

package models; 
import java.util.*; 
import javax.persistence.*; 
import play.db.jpa.*; 

@Entity 
@Table(name = "commitment_type_value") 
public class CommittmentTypeValue extends Model{  
    @Id 
    @Column(name = "id", nullable = false) 
    public Long id;  
    @Column(name = "value", nullable = true) 
    public String type;  
    @ManyToOne 
    @JoinColumn(name="commitment_type_id") 
    public CommitementType commitmentType;  
    public CommittmentTypeValue(){ 

    } 
} 

------------- 


package models; 

import java.util.*; 

import javax.persistence.*; 

import play.db.jpa.*; 


/** 
* 
* @author hatem 
*/ 
@Entity 
@Table(name = "commitment_type") 
public class CommitementType extends Model{ 

    @Id 
    @Column(name = "id", nullable = false) 
    public Long id; 

    @Column(name = "type", nullable = true) 
    public String type; 

    @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="commitmentType") 
    public List<CommittmentTypeValue> commitmentTypeValues; 

    public CommitementType(){ 

    } 
} 

當我執行我的應用程序,出現這樣的問題:發生

一個JPA錯誤(無法建立EntityManagerFactory的):一個外國 鍵引用Models.CommitementType.CommittmentTypeValue 的列數錯誤。應該是2

請,可以任何人告訴我什麼是錯的?

回答

3

請檢查您的外鍵列的名稱應該能夠完全匹配列的名稱。


編輯 如果您的問題仍然沒有解決,那麼請檢查您的persistance.xml有

<property name="generateDdl" value="true" /> 

,如果它已經有那麼檢查您是否在代表得到任何錯誤。

如果是,那麼清除數據表中的配置文件 或 增減和創建-tables選項或 更改您的代碼如下

@ManyToOne(optional=true) 
@JoinColumn(name="commitment_type_id", nullable = true) 
public CommitementType commitmentType; 

因爲你可能在表中舊數據這可能會阻止創建新表格。

+0

我不明白,我不是還沒有在創建數據庫中的表,它與遊戲的框架內聯網應用,如果我跑我的應用程序,表格自動創建 –

+0

是üR右,他要利用生成的實體類。 – vels4j

0

參考列名中缺少您的多對一類加入CommittmentTypeValue

@ManyToOne 
@JoinColumn(name="commitment_type_id" referencedColumnName="id") 
public CommitementType commitmentType; 

同時指定目標實體

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, targetEntity=CommittmentTypeValue.class, mappedBy="commitmentType") 
public List<CommittmentTypeValue> commitmentTypeValues; 
+0

現在的錯誤是:發生 JPA錯誤 一個JPA錯誤(無法建立的EntityManagerFactory):引用的mappedBy的未知目標實體屬性:models.CommittmentTypeValue.commitementType在models.CommitementType.commitmentTypeValues –

+0

檢查更新現在 – vels4j

+0

你使用任何IDE? – vels4j

0

錯誤聽起來像是你標識在CommitementType是複合材料,所以你的外國鍵必須包含兩列。

包括了CommitementType的代碼。