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();
}
hibernate创建的查询用于获取以下详细信息:
select
this_.id as id1_4_4_,
this_.code_melli as code_mel2_4_4_,
this_.email as email3_4_4_,
this_.mobile as mobile4_4_4_,
this_.name as name5_4_4_,
this_.phone as phone6_4_4_,
this_.register_date as register7_4_4_,
attend1_.course_id as course_i1_1_0_,
attend1_.student_id as student_2_1_0_,
attend1_.score as score3_1_0_,
course2_.id as id1_2_1_,
course2_.cluster as cluster2_2_1_,
course2_.code as code3_2_1_,
course2_.end_date as end_date4_2_1_,
course2_.name as name5_2_1_,
course2_.salary as salary6_2_1_,
course2_.start_date as start_da7_2_1_,
course2_.students as students8_2_1_,
course2_.teacher_id as teacher10_2_1_,
course2_.tuition as tuition9_2_1_,
schedule3_.course_id as course_i1_2_6_,
schedule3_.course_id as course_i1_3_6_,
schedule3_.day as day2_3_6_,
schedule3_.course_id as course_i1_3_2_,
schedule3_.day as day2_3_2_,
schedule3_.endt as endt3_3_2_,
schedule3_.start as start4_3_2_,
teacher8_.id as id1_5_3_,
teacher8_.code_melli as code_mel2_5_3_,
teacher8_.email as email3_5_3_,
teacher8_.mobile as mobile4_5_3_,
teacher8_.name as name5_5_3_,
teacher8_.phone as phone6_5_3_,
teacher8_.register_date as register7_5_3_
from
public.students this_
inner join
public.attend attend1_
on this_.id=attend1_.student_id
inner join
public.courses course2_
on attend1_.course_id=course2_.id
left outer join
public.schedule schedule3_
on course2_.id=schedule3_.course_id
left outer join
public.teachers teacher8_
on course2_.teacher_id=teacher8_.id
where
this_.id=?
但它有一个问题:
setFetchMode方法不起作用。因为程序引发以下异常:
引发了“org.hibernate.LazyInitializationException”异常。