我正在开发一个hibernate,spring项目来帮助我理解这两者的基础知识。 我遇到了一个问题,我希望能够在表格中添加外键。
我一直在浏览互联网以获取有关此主题的信息,但我找不到符合我需求的内容。
我有两个班级:
Schools
Classes
现在我想将主键从Schools映射到Classes。
这是我现在的代码:
@ManyToOne
@JoinColumn(name = "SCHOOL_ID", table = "SCHOOL")
private School school;
和我的吸气者和二传手:
public long getSchool() {
return school.getId();
}
public void setSchool(long schoolId) {
this.school.setId(schoolId);
}
这是要走的路吗?或者我完全以错误的方式看待它。
谢谢!
答案 0 :(得分:9)
你是在正确的轨道上,虽然它更好地处理实际的对象而不是ids,例如。
@ManyToOne
@JoinColumn(name = "SCHOOL_ID", table = "SCHOOL")
private School school;
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school=school;
}
答案 1 :(得分:1)
将其更改为:
public long getSchool() {
return this.school;
}
public void setSchool(School school) {
this.school = school;
}