我正在使用hibernate开发学生信息系统,学生可以拥有多个课程(和标记)。我想要我的POJO:
学生:student attributes + Set<Course>
课程:course attributes + int marks
但是在数据库中,我遵循了3个表。
create table STUDENT (
STUDENT_ID BIGINT not null auto_increment primary key,
// ... student type attributes
)
create table COURSE (
COURSE_ID BIGINT not null auto_increment primary key,
// ... course type attributes
)
create table STUDENT_COURSE_MARKS (
STUDENT_ID BIGINT not null,
COURSE_ID BIGINT not null,
MARKS int default 0,
CHECK (MARKS <= 100)
)
问题:
答案 0 :(得分:2)
这是简单的映射:
@Entity
@Table(name = "STUDENT")
public Class Student {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
private Set<StudentCourseMark> studentCourseMark = new HashSet<StudentCourseMark>(0);
}
@Entity
@Table(name = "COURSE")
public Class Course {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "course")
private Set<StudentCourseMark> studentCourseMark = new HashSet<StudentCourseMark>(0);
}
@Entity
@Table(name = "STUDENT_COURSE_MARKS")
public Class StudentCourseMark {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STUDENT_ID")
private Student student;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "COURSE_ID")
private Course course;
private List<Mark> marks = new ArrayList<Mark>(0);
}
当然,您可以在STUDENT_ID
的{{1}},COURSE_ID
上使用PrimaryKey,例如:
http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
如何获得学生成绩:
StudentCourseMark