31hibernate_fetch_2_join
hibernate抓取策略(单端代理的批量抓取)
设置fetch="join",如:
<many-to-one name="classes" column="classesid" fetch="join"/>
fetch="join",hibernate会通过select语句使用外连接来加载其关联实体或集合
此时lazy会失效
===================================================================================
public void testFetch1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
System.out.println("classes.name=" + student.getClasses().getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
测试结果:
Hibernate: select student0_.id as id1_1_, student0_.name as name1_1_, student0_.classesid as classesid1_1_, classes1_.id as id0_0_, classes1_.name as name0_0_ from t_student student0_ left outer join t_classes classes1_ on student0_.classesid=classes1_.id where student0_.id=?
student.name=班级0的学生0
classes.name=班级0
设置fetch="join",如:
<many-to-one name="classes" column="classesid" fetch="join"/>
fetch="join",hibernate会通过select语句使用外连接来加载其关联实体或集合
此时lazy会失效
===================================================================================
public void testFetch1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
System.out.println("classes.name=" + student.getClasses().getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
测试结果:
Hibernate: select student0_.id as id1_1_, student0_.name as name1_1_, student0_.classesid as classesid1_1_, classes1_.id as id0_0_, classes1_.name as name0_0_ from t_student student0_ left outer join t_classes classes1_ on student0_.classesid=classes1_.id where student0_.id=?
student.name=班级0的学生0
classes.name=班级0