'univ_orientation.dz.Entity.cursusGlobale.Student_idStudent'上的@OneToOne或@ManyToOne引用了未知实体:int

时间:2019-05-23 14:57:20

标签: java jpa

我正在使用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;
}


}

1 个答案:

答案 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提供的抽象的一部分。

参考:JPA Hibernate One-to-One relationship