我想用休眠连接4个表。我编写以下代码以获取课程名称,教师详细信息和课程表(按学生ID): 我的学生实体是:
@Entity
@Table(name = "students", schema = "public")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "students_id_seq")
@SequenceGenerator(name = "students_id_seq", sequenceName = "students_id_seq", allocationSize = 1)
@Column(name = "id", nullable = false, unique = true)
private int id;
@Column(name = "name", nullable = false, length = 60)
private String name;
@Column(name = "code_melli", nullable = false, length = 10)
private String codeMelli;
@Column(name = "register_date", nullable = false)
private Date registerDate;
@Column(name = "mobile", length = 11)
private String mobile;
@Column(name = "phone", length = 15)
private String phone;
@Column(name = "email", length = 50)
private String email;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", referencedColumnName = "id", insertable = false, updatable = false)
private List<Attend> attends;
确定学生参加课程的我的参加实体,他的分数是:
@Entity
@Table(name = "attend", schema = "public")
public class Attend {
@EmbeddedId
AttendKey attendKey;
@Column(name = "score")
private int score;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", referencedColumnName = "id", insertable = false, updatable = false)
private Student student;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "course_id", referencedColumnName = "id", insertable = false, updatable = false)
private Course course;
映射课程详细信息的课程实体为:
@Entity
@Table(name = "courses", schema = "public")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "courses_id_seq")
@SequenceGenerator(name = "courses_id_seq", sequenceName = "courses_id_seq", allocationSize = 1)
@Column(name = "id", nullable = false, unique = true)
private int id;
@Column(name = "code")
private int code;
@Enumerated(EnumType.ORDINAL)
@Column(name = "cluster", nullable = false)
private ClusterType cluster;
@Column(name = "name", nullable = false, length = 40)
private String name;
@OneToOne
@JoinColumn(name = "teacher_id")
private Teacher teacher;
@Column(name = "students", nullable = false)
private int students;
@Column(name = "salary", nullable = false)
private int salary;
@Column(name = "tuition", nullable = false)
private int tuition;
@Column(name = "start_date")
private Date startDate;
@Column(name = "end_date")
private Date endDate;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "course_id", referencedColumnName = "id", insertable = false, updatable = false)
private List<Schedule> scheduleList;
在一周的某一天保存课程开始和结束时间的时间表实体是:
@Entity
@Table(name = "schedule", schema = "public")
@IdClass(ScheduleKey.class)
public class Schedule {
@Id
@Column(name = "course_id")
private int course_id;
@Id
@Column(name = "day")
@Enumerated(EnumType.ORDINAL)
private Day day;
@Column(name = "start", nullable = false)
private Time start;
@Column(name = "endt", nullable = false)
private Time endt;
我该如何使用休眠标准API将这些表连接起来并获取特定学生的课程表。
用于获取数据的SQL查询:
select
*
from
students
inner join
attend
on students.id=attend.student_id
inner join
courses
on attend.course_id=course.id
left outer join
schedule_
on course.id=schedule.course_id
left outer join
public.teachers
on course.teacher_id=teacher.id
where
students.id=102552
我使用
public Student getStudentDetail(int studentId) {
Criteria criteria = createEntityCriteria();
criteria.add(Restrictions.eq("id", studentId));
criteria.setFetchMode("attends", FetchMode.JOIN);
Criteria attendCriteria = criteria.createCriteria("attends", "attend")
.setFetchMode("course", FetchMode.JOIN);
Criteria courseCriteria = attendCriteria.createCriteria("course", "course")
.setFetchMode("scheduleList", FetchMode.JOIN);
Criteria scheduleCriteria = courseCriteria.createCriteria("scheduleList", "schedule");
return (Student) scheduleCriteria.uniqueResult();
}