Я хочу объединить 4 таблицы с Hibernate.я пишу следующий код для получения названия курса, детали курса учителя и расписания курсов по идентификатору студента: моя сущность студента:
@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();
}