35hibernate_fetch_6_batch_size
hibernate抓取策略,,batch-szie在<class>上的应用
batch-size属性,可以批量加载实体类,参见:Classes.hbm.xml
<class name="Classes" table="t_classes" batch-size="3">
=============================================================================================
public void testFetch1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
List students = session.createQuery("select s from Student s where s.id in(:ids)")
.setParameterList("ids", new Object[]{1, 11, 21, 31, 41, 51, 61, 71, 81, 91})
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
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);
}
}
配置batch-size="3"时:<class name="Classes" table="t_classes" batch-size="3">
Hibernate: select student0_.id as id1_, student0_.name as name1_, student0_.classesid as classesid1_ from t_student student0_ where student0_.id in (? , ? , ? , ? , ? , ? , ? , ? , ? , ?)
student.name=班级0的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?)
classes.name=班级0
student.name=班级1的学生0
classes.name=班级1
student.name=班级2的学生0
classes.name=班级2
student.name=班级3的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?)
classes.name=班级3
student.name=班级4的学生0
classes.name=班级4
student.name=班级5的学生0
classes.name=班级5
student.name=班级6的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?)
classes.name=班级6
student.name=班级7的学生0
classes.name=班级7
student.name=班级8的学生0
classes.name=班级8
student.name=班级9的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级9
===============================================================================================
什么都不配置时:发10条<class name="Classes" table="t_classes">
Hibernate: select student0_.id as id1_, student0_.name as name1_, student0_.classesid as classesid1_ from t_student student0_ where student0_.id in (? , ? , ? , ? , ? , ? , ? , ? , ? , ?)
student.name=班级0的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级0
student.name=班级1的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级1
student.name=班级2的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级2
student.name=班级3的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级3
student.name=班级4的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级4
student.name=班级5的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级5
student.name=班级6的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级6
student.name=班级7的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级7
student.name=班级8的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级8
student.name=班级9的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级9
=============================================================================================
配置batch-size="5"时:发2条
<class name="Classes" table="t_classes" batch-size="5">
Hibernate: select student0_.id as id1_, student0_.name as name1_, student0_.classesid as classesid1_ from t_student student0_ where student0_.id in (? , ? , ? , ? , ? , ? , ? , ? , ? , ?)
student.name=班级0的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?, ?, ?)
classes.name=班级0
student.name=班级1的学生0
classes.name=班级1
student.name=班级2的学生0
classes.name=班级2
student.name=班级3的学生0
classes.name=班级3
student.name=班级4的学生0
classes.name=班级4
student.name=班级5的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?, ?, ?)
classes.name=班级5
student.name=班级6的学生0
classes.name=班级6
student.name=班级7的学生0
classes.name=班级7
student.name=班级8的学生0
classes.name=班级8
student.name=班级9的学生0
classes.name=班级9
batch-size属性,可以批量加载实体类,参见:Classes.hbm.xml
<class name="Classes" table="t_classes" batch-size="3">
=============================================================================================
public void testFetch1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
List students = session.createQuery("select s from Student s where s.id in(:ids)")
.setParameterList("ids", new Object[]{1, 11, 21, 31, 41, 51, 61, 71, 81, 91})
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
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);
}
}
配置batch-size="3"时:<class name="Classes" table="t_classes" batch-size="3">
Hibernate: select student0_.id as id1_, student0_.name as name1_, student0_.classesid as classesid1_ from t_student student0_ where student0_.id in (? , ? , ? , ? , ? , ? , ? , ? , ? , ?)
student.name=班级0的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?)
classes.name=班级0
student.name=班级1的学生0
classes.name=班级1
student.name=班级2的学生0
classes.name=班级2
student.name=班级3的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?)
classes.name=班级3
student.name=班级4的学生0
classes.name=班级4
student.name=班级5的学生0
classes.name=班级5
student.name=班级6的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?)
classes.name=班级6
student.name=班级7的学生0
classes.name=班级7
student.name=班级8的学生0
classes.name=班级8
student.name=班级9的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级9
===============================================================================================
什么都不配置时:发10条<class name="Classes" table="t_classes">
Hibernate: select student0_.id as id1_, student0_.name as name1_, student0_.classesid as classesid1_ from t_student student0_ where student0_.id in (? , ? , ? , ? , ? , ? , ? , ? , ? , ?)
student.name=班级0的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级0
student.name=班级1的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级1
student.name=班级2的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级2
student.name=班级3的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级3
student.name=班级4的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级4
student.name=班级5的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级5
student.name=班级6的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级6
student.name=班级7的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级7
student.name=班级8的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级8
student.name=班级9的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
classes.name=班级9
=============================================================================================
配置batch-size="5"时:发2条
<class name="Classes" table="t_classes" batch-size="5">
Hibernate: select student0_.id as id1_, student0_.name as name1_, student0_.classesid as classesid1_ from t_student student0_ where student0_.id in (? , ? , ? , ? , ? , ? , ? , ? , ? , ?)
student.name=班级0的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?, ?, ?)
classes.name=班级0
student.name=班级1的学生0
classes.name=班级1
student.name=班级2的学生0
classes.name=班级2
student.name=班级3的学生0
classes.name=班级3
student.name=班级4的学生0
classes.name=班级4
student.name=班级5的学生0
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id in (?, ?, ?, ?, ?)
classes.name=班级5
student.name=班级6的学生0
classes.name=班级6
student.name=班级7的学生0
classes.name=班级7
student.name=班级8的学生0
classes.name=班级8
student.name=班级9的学生0
classes.name=班级9