在hibernate中设置:
查询数据库每次最多返回50条结果:
<property name=”hibernate.jdbc.fetch_size”>50</property>
30条更新数据库一次:
<property name= ”hibernatejdbc.batch_size”>30</property>
但不是所有的数据库都支持的,sqlServer 和orcale 都支持的。
一.hibernate 抓取策略(Fetch) 单端代理批量抓取
1.fetch=”select” 关联实体
//fetch 默认是select
<many-to-one name="businessId" column="business_id" insert="true" update="true" fetch="select">
Student student = (Student)session.load(Student.class,1);
System.out.println(student.getName());
System.out.println(student.getClasses().getName()); //多对一中的属性班级,获取班级名称
Fetch=”select” ,上面程序会发2条sql语句,第二条发送一条select语句抓取当前对象关联实体或集合 (这里指是班级名称)
2.fetch=” join” 关联实体
//fetch 设置成 join
<many-to-one name="businessId" column="business_id" insert="true" update="true"
fetch=" join ">
Student student = (Student)session.load(Student.class,1);
System.out.println(student.getName());
System.out.println(student.getClasses().getName()); //多对一中的属性班级,获取班级名称
fetch=” join” , 上面程序会发1条sql语句, hibernate会通过select使用外链接来加载其关联实体或集合,此时lazy会失效
二.hibernate 抓取策略(Fetch) 集合代理批量抓取
1. fetch=”select”
//fetch 默认是select
<set name="students" inverse="true" cascade="all" fetch="select">
<key column="classid" />
<one-to-many class="com.Student" />
</set>
Classes cla = (Classes)session.load(Classes.class,1);
System.out.println(cla.getName());
for(Iterator iter = cla.getStudents().iterator();iter.hasNext();){
Student student = (Student)iter.next();
System.out.println(student.getName());
}
fetch=”select” ,上面程序用到了就发sql语句,第二从循环中发了N条,如果:fetch=”subselect”,则只是发送一条语句,见下面
2 fetch=”join”
//fetch 设置成join
<set name="students" inverse="true" cascade="all" fetch="join">
<key column="classid" />
<one-to-many class="com.Student" />
</set>
Classes cla = (Classes)session.load(Classes.class,1);
System.out.println(cla.getName());
for(Iterator iter = cla.getStudents().iterator();iter.hasNext();){
Student student = (Student)iter.next();
System.out.println(student.getName());
}
Fetch=”select” ,上面程序只发了一条sql语句
三.hibernate 抓取策略(Fetch) 集合代理批量抓取
2. fetch=”subselect”
//fetch设置成subselect
<set name="students" inverse="true" cascade="all" fetch="subselect">
<key column="classid" />
<one-to-many class="com.Student" />
</set>
List classesList = session.createQuery(“select c from Classes c where c.id in(1,2,3)”);
for(Iterator iter=classesList.iterator();iter.hasNext();){
Classess classess = (Classess)iter.next();
System.out.println(“classes.name=”+ classes.getName());
for(Iterator iter1 = classess.getStudents().iterator();iter1.hasNext();){
Student student = (Student)iter1.next();
System.out.println(student.getName());
}
}
Fetch=”subselct” ,另外发送一条select语句抓取在前面查询到的所有实体对象的关联集合
四.hibernate 批量策略batch-size属性,可以批量加载实体类,
参见Classes.hbm.xml,同时集合也可以是使用,如:
<set name="students" inverse="true" cascade="save-update" batch-size=”5”>
在配置文件hbm.xml 设置中:
<class name="com.Classes " table="tb_classes" batch-size=”3”>
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 = (Sutdent)iter.next();
System.out.println(student.getName());
System.out.println(student.getClassess().getName());
}
当第二个for循环时,每次加载 之前设置的 数量 实体对象, 如:现在设置3,那么当Iterator iter=students.iterator();iter.hasNext();时候,他会查询出3个student对象