1、显而易见的概念
懒加载——也被称为延迟加载,它在查询的时候不会立刻访问数据库,而是返回代理对象,当真正去使用对象的时候才会访问数据库。
急加载——就是只要我们实体做了关联,全部一次性执行完所有的SQL语句
2、@OneToMany
****默认为懒加载****
@Test
public void find(){
Session s= sessionFactory.openSession();
List<TeacherEntity> teacher= s.createQuery("from TeacherEntity").list();
for(TeacherEntity t:teacher){
t.getTname();
List<StudentEntity> lists= t.getStuList();
for(StudentEntity stu:lists){
System.out.println(stu.getSname());
}
}
s.close();
}
懒加载结果:
Hibernate: select teacherent0_.t_id as t_id1_5_, teacherent0_.t_name as t_name2_5_ from t_teacher2 teacherent0_
Hibernate: select stulist0_.tid as tid3_4_0_, stulist0_.s_id as s_id1_4_0_, stulist0_.s_id as s_id1_4_1_, stulist0_.s_name as s_name2_4_1_, stulist0_.tid as tid3_4_1_ from t_student2 stulist0_ where stulist0_.tid=? <