我正在使用Hibernate 5和SpringMVC在Web应用程序中工作,当我只使用简单表(没有任何表之间的关系)时,一切正常,更新,保存,删除...,但是如果我尝试添加表之间的一些关系,以及在映射时出现此错误:
@OneToOne or @ManyToOne on 'univ_orientation.dz.Entity.cursusGlobale.Student_idStudent' references an unknown entity: int
我曾经搜索过以解决此问题,但发现这是一个配置问题,Hibernate无法将类cursusGlobale
识别为实体。
我的问题是,为什么在使用关系时Hibernate不能将类cursusGlobale
识别为一个实体,但是如果我只使用没有表之间关系的简单表呢?
package univ_orientation.dz.Entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="cursusGlobale")
public class cursusGlobale {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="idcursusGlobale")
private int idcursusGlobale;
@Column(name="nbrRedoubler")
private int nbrRedoubler;
@Column(name="nbrDette")
private int nbrDette;
@Column(name="idStudent")
private int idStudent;
@OneToOne( cascade= CascadeType.ALL)
@JoinColumn(name="Student_idStudent")
private int Student_idStudent;
public cursusGlobale() {
}
public cursusGlobale(int nbrRedoubler, int nbrDette, int
student_idStudent) {
super();
this.nbrRedoubler = nbrRedoubler;
this.nbrDette = nbrDette;
idStudent = student_idStudent;
}
public int getIdcursusGlobale() {
return idcursusGlobale;
}
public void setIdcursusGlobale(int idcursusGlobale) {
this.idcursusGlobale = idcursusGlobale;
}
public int getNbrRedoubler() {
return nbrRedoubler;
}
public void setNbrRedoubler(int nbrRedoubler) {
this.nbrRedoubler = nbrRedoubler;
}
public int getNbrDette() {
return nbrDette;
}
public void setNbrDette(int nbrDette) {
this.nbrDette = nbrDette;
}
public int getIdStudent() {
return idStudent;
}
public void setIdStudent(int idStudent) {
this.idStudent = idStudent;
}
}
答案 0 :(得分:0)
您尚未创建关系。您正在使用Java本机类型int
而不是其他类Student
。
@OneToOne( cascade= CascadeType.ALL)
@JoinColumn(name="Student_idStudent")
private int Student_idStudent;
如错误消息清楚所述:@OneToOne or @ManyToOne on 'univ_orientation.dz.Entity.cursusGlobale.Student_idStudent' references an unknown entity: int
, Student_idStudent 被定义为int,而不是实体。您将需要更多类似的东西
@OneToOne( cascade= CascadeType.ALL)
@JoinColumn(name="Student_idStudent")
private Student student;
即使它是一个类,也将作为Foreign Key
保留在数据库中的Student
表中。这是JPA提供的抽象的一部分。