hibernate抓取策略(集合代理的批量抓取) 设置fetch= " join ",如: < set name= " students " inverse= " true " cascade= " all " fetch= " join "> fetch= " join ",hibernate会通过select语句使用外连接来加载其关联实体或集合 此时lazy会失效 ============================================================================================ public void testFetch1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Classes classes = (Classes)session.load(Classes. class, 1); System. out.println( " classes.name= " + classes.getName()); for (Iterator iter=classes.getStudents().iterator(); iter.hasNext();) { Student student = (Student)iter.next(); System. out.println( " student.name= " + student.getName()); } session.getTransaction().commit(); } catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } 测试结果: Hibernate: select classes0_.id as id0_1_, classes0_.name as name0_1_, students1_.classesid as classesid3_, students1_.id as id3_, students1_.id as id1_0_, students1_.name as name1_0_, students1_.classesid as classesid1_0_ from t_classes classes0_ left outer join t_student students1_ on classes0_.id=students1_.classesid where classes0_.id=? classes.name=班级0 student.name=班级0的学生0 student.name=班级0的学生5 student.name=班级0的学生3 student.name=班级0的学生1 student.name=班级0的学生2 student.name=班级0的学生4 student.name=班级0的学生6 ------------------------- 0 5 3 1 2 4 6---------------------------------------------------- 因为是set,所以是无序的: Hibernate: select classes0_.id as id0_1_, classes0_.name as name0_1_, students1_.classesid as classesid3_, students1_.id as id3_, students1_.id as id1_0_, students1_.name as name1_0_, students1_.classesid as classesid1_0_ from t_classes classes0_ left outer join t_student students1_ on classes0_.id=students1_.classesid where classes0_.id=? classes.name=班级0 student.name=班级0的学生3 student.name=班级0的学生6 student.name=班级0的学生1 student.name=班级0的学生2 student.name=班级0的学生5 student.name=班级0的学生0 student.name=班级0的学生4 ------------------------- 3 6 1 2 5 0 4-------------------------------------------------