2013-08-22 59 views
0

我想創建我的JPA和MYSQL使用播放框架,但映射@OneToMany的PersistenceException:使用@OneToMany

PersistenceException: Error with the Join on [models.Patient.progress]. 
    Could not find the matching foreign key for [id] in table[Results]? 
    Perhaps using a @JoinColumn with the name/referencedColumnName attributes swapped? 

我的類看起來如下當我'收到以下錯誤數據庫時:

患者

@Entity 
@Table(name = "Patients") 
public class Patient 
    extends User { 


@Id 
@Column(name = "idPatient") 
private int idPatient; 
@Constraints.Required 
private String medicalCoverage; 
@Constraints.Required 
private String disease; 
@Constraints.Required 
private int gradeDisease; 
@OneToMany(cascade = CascadeType.ALL, mappedBy = "patient", 
     fetch = FetchType.LAZY) 
private List<Results> progress; 
@ManyToMany 
@JoinTable(name = "therapist_relation", 
     joinColumns = {@JoinColumn(name = "idPatient")}, 
     inverseJoinColumns = {@JoinColumn(name = "idTherapist")}) 
private List<Therapist> therapists; 
private int qAwardA; 
private int qAwardB; 
private int qAwardC; 

結果

@Entity 
@Table(name = "Results") 
public class Results { 

    @Id 
    @Column(name = "idResult") 
    private int idResult; 
    private Game game; 
    @ManyToOne(optional = false, fetch = FetchType.LAZY) 
    @JoinColumn(name="idPatient", referencedColumnName = "idPatient", nullable = false) 
    private Patient patient; 

    @OneToMany 
    @JoinColumn(name="idTherapist", referencedColumnName = "idResult") 
    private Therapist therapist; 
    private int punctuation; 
    private String description; 

我的代碼有什麼問題?

回答

0

問題是與在Results類@JoinColumn註解爲Patient

您正在使用referencedColumnName參數。

下列文件:

(Optional) The name of the column referenced by this foreign key column.

因此,在這種情況下,這種說法是沒有必要的。如果你想使用它應該看起來像referencedColumnName = "idResult",但正如我所說,這是沒有必要的。

+0

如果我讓你說我有這個新的錯誤:PersistenceException:與[models.Results.patient]上的連接錯誤。無法找到[null]的本地匹配可能是@JoinColumn中的錯誤 – user1371176

相關問題