Hibernate - 学生评分系统。需要关于注释的帮助

时间:2013-08-29 12:26:32

标签: java hibernate database-design annotations nhibernate-mapping

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

问题:

  1. 我是否需要为每个数据库表创建一个pojo&gt;
  2. 如何设置注释以实现此功能?

1 个答案:

答案 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
相关问题