【发布时间】:2014-09-04 19:36:04
【问题描述】:
我正在学习 JPA 2.1。我使用 sql 设计了一个数据库,但无法使用 JPA 2.1 做同样的事情。我列出了 sql 脚本。
create table SchoolClass (classNo int, constraint schoolClassPK Primary Key (classNo));
create table Section (classNo int, sectionId varchar(4), constraint sectionPK Primary Key(classNo, sectionId), constraint sectionClassNoFK Foreign Key (classNo) References SchoolClass(classNo));
create table Student (classNo int, sectionId varchar(4), rollNo int, name varchar(30), constraint studentPK Primary Key (classNo, sectionId, rollNo), constraint studentClassNoFK Foreign Key (classNo, sectionId) References Section(classNo, sectionId));
我的问题是在实现学生课程。这是我尝试用 JPA 做的事情:
@Entity
public class Schoolclass implements Serializable {
@Id
private int classNo;
//other members follow
}
// IdClass
public class SectionCK implements Serializable {
private int classNo;
private String sectionId;
// hashCode(), equals(Object), other members follow
}
@Entity
@IdClass(SectionCK.class)
public class Section implements Serializable {
@Id
private int sectionPK;
@Id
private String sectionId;
@Id
@ManyToOne(cascade = { PERSIST, MERGE })
@JoinColumn(name="SchoolClass_ClassNo", referencedColumnName="classNo")
private SchoolClass classNo;
//other members follow
}
// IdClass
public class StudentCK implements Serializable {
private int classNo;
private String sectionId;
private int rollNo;
// hashCode(), equals(Object), other members follow
}
@Entity
@IdClass(StudentCK.class)
public class Student implements Serializable {
@Id
private int rollNo;
@Column(name="name")
private String name;
@Id
@ManyToOne(cascade = { PERSIST, MERGE })
@JoinColumns({
@JoinColumn(name="Section_SectionId", referencedColumnName="sectionId"),
@JoinColumn(name="Section_ClassNo", referencedColumnName="classNo")
})
private Section sectionId;
//other members follow
}
但这无法生成上述数据库。需要进行哪些更改?
【问题讨论】:
-
我是否在您的 SQL 代码中遗漏了什么?对于表
Section,您无需定义classSection列,而是在PK 中引用它。与表相同Student:classSection在哪里?在Student中,您将classNo从Section引用为FK。这样的列只存在于SchoolClass。 -
classNo没问题。只有classSection坚持 -
您能详细说明失败的原因吗? zbigniewTomczak 的答案似乎是正确的,所以如果它不起作用,请显示什么错误或它产生了什么。您在上面的示例中加入列似乎是错误的,因为 Section 表中没有“SchoolClass_ClassNo”字段 - 如果这是使用的外键字段,它应该是“classNo”。在 Student 映射中定义的 joincolumns 存在相同的问题 - 它们与学生表中所需的字段不匹配。
-
@zbigniewTomczak 感谢您指出 SQL 脚本的问题,我已纠正。
标签: jpa jdbc orm eclipselink dao