Hibernate检索策略学习之--批量加载

chenssss 2010-11-08

所谓批量加载,即把原本要发送的SQL分批统一发送,比如说原本要发送100条SQL,如果设置batch-size=3,则只需要发送100/3+1=34条,可以提高效率

批量加载分为延迟加载和立即加载,先说立即加在

首先建立测试数据库

CREATE TABLE certificate (

idvarchar(100)NOTNULLdefault'',

descriptionvarchar(100)default'',

PRIMARYKEY(id)

);

CREATE TABLE student (

team_idvarchar(100)default'',

idvarchar(100)NOTNULLdefault'',

namevarchar(20)default'',

cardIdvarchar(20)NOTNULLdefault'',

ageint(11)default'0',

PRIMARYKEY(id)

);

CREATETABLEteam(

idvarchar(100)NOTNULLdefault'',

teamNamevarchar(100)default'',

PRIMARYKEY(id)

);

INSERTINTOstudentVALUES

('5','1','spark','200211332',13),

('4','2','jerry','200233332',23),

('3','3','adidas','200231332',33),

('2','4','zhouxingchi','200231132',43),

('1','5','tomclus','200512345',53),

('1','6','tom','200511345',63);

INSERT INTO team VALUES

('5','team5'),

('4','team4'),

('3','team3'),

('2','team2'),

('1','team1');

INSERT INTO certificate VALUES

('1','card1'),

('2','card2'),

('3','card3'),

('4','card4'),

('5','card5'),

('6','card6');

建立POJO对象

package Search.immediately;

public class Certificate ............{

privateStringid;

privateStringdescription;

    private Student stu;

    public Student getStu() ............{

returnstu;

    }

    public void setStu(Student stu) ............{

this.stu=stu;

    }

publicStringgetDescription()............{

returndescription;

    }

    public void setDescription(String description) ............{

this.description=description;

    }

    public String getId() ............{

returnid;

    }

    public void setId(String id) ............{

this.id=id;

}

}

package Search.immediately;

import java.util.HashSet;import java.util.Set;

publicclassTeam............{

privateStringid;

privateSetstudents=newHashSet();

privateStringteamName;

privateSettests;

publicSetgetTests()............{

returntests;

}

publicvoidsetTests(Settests)............{

this.tests=tests;

    }

    public String getId() ............{

returnid;

    }

    public void setId(String id) ............{

this.id=id;

    }

    public String getTeamName() ............{

returnteamName;

    }

    public void setTeamName(String name) ............{

this.teamName=name;

    }

    public Set getStudents() ............{

returnstudents;

    }

    public void setStudents(Set students) ............{

this.students=students;

}

}

package Search.immediately;

public class Certificate ............{

privateStringid;

privateStringdescription;

    private Student stu;

    public Student getStu() ............{

returnstu;

    }

    public void setStu(Student stu) ............{

this.stu=stu;

    }

publicStringgetDescription()............{

returndescription;

    }

    public void setDescription(String description) ............{

this.description=description;

    }

    public String getId() ............{

returnid;

    }

    public void setId(String id) ............{

this.id=id;

}

}

HBM文件

student.hbm.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

MappingfileautogeneratedbyMyEclipse-HibernateTools

-->

<hibernate-mapping package="Search.batch.immediately" >

    <class name="Student" table="student" lazy="false">

<idname="id"column="id"unsaved-value="null">

<generatorclass="uuid.hex"></generator>

       </id>

       <property name="cardId" column="cardId"></property>

<propertyname="name"column="name"></property>

<propertyname="age"column="age"></property>

<!--为了设置student-certificate的延迟1对1,设置class的lazy="no-proxy"-->

<one-to-onename="cer"

class="Search.batch.immediately.Certificate"

constrained="true"

lazy="no-proxy"

outer-join="false"

cascade="all">

</one-to-one>

<many-to-onename="team"

column="team_id"

outer-join="false"

class="Search.batch.immediately.Team"></many-to-one>

</class>

</hibernate-mapping>

Certificate.hbm.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

MappingfileautogeneratedbyMyEclipse-HibernateTools

-->

<hibernate-mappingpackage="Search.batch.immediately">

<!--为了设置student-certificate的延迟1对1,设置class的lazy="true"-->

<classname="Certificate"table="certificate"lazy="true">

<idname="id"column="id">

<generatorclass="foreign">

<paramname="property">stu</param>

</generator>

       </id>

<propertyname="description"column="description"></property>

<one-to-onename="stu"

class="Search.batch.immediately.Student"

outer-join="false"

constrained="true">

</one-to-one>

</class>

</hibernate-mapping>

Team.hbm.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

MappingfileautogeneratedbyMyEclipse-HibernateTools

-->

<hibernate-mappingpackage="Search.batch.immediately">

<classname="Team"table="team"lazy="false">

<idname="id"column="id">

<generatorclass="uuid.hex"></generator>

</id>

<propertyname="teamName"column="teamName"></property>

<!--需要批量加载,设置batch-size="2"-->

<setname="students"

lazy="false"

inverse="true"

outer-join="false"

>

<keycolumn="team_id"></key>

<one-to-manyclass="Search.batch.immediately.Student"/>

</set>

</class>

</hibernate-mapping>

测试代码:

package Search.batch.immediately;

import java.io.File;import java.util.List;

import org.hibernate.Query;

importorg.hibernate.Session;

importorg.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class Test ...{

publicstaticvoidmain(String[]args)...{

Teamteam=null;

StringfilePath=System.getProperty("user.dir")+File.separator+"src/Search/batch/immediately"+File.separator+"hibernate.cfg.xml";

Filefile=newFile(filePath);

SessionFactorysessionFactory=newConfiguration().configure(file).buildSessionFactory();

Sessionsession=sessionFactory.openSession();

Queryquery=session.createQuery("fromTeamt");

        List resultList=query.list();

}

运行,结果如下:

Hibernate: select team0_.id as id2_, team0_.teamName as teamName2_ from team team0_

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id=

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id=?

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id=?

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id=?

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id=?

有5条查询student的SQL(本身有6个学生,但有两个学生属于一个team)可以看到,这是典型的立即加在,取得team对象后,把team所包含的student取出,如果有100个学生,则会执行100个SQL,这是很没有效率的

我们修改Team的hbm文件,为其配置批量加载student的配置(1对多在set标签上配置,多对1在class标签上配置)

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

MappingfileautogeneratedbyMyEclipse-HibernateTools

-->

<hibernate-mappingpackage="Search.batch.immediately">

<classname="Team"table="team"lazy="false">

<idname="id"column="id">

<generatorclass="uuid.hex"></generator>

</id>

<propertyname="teamName"column="teamName"></property>

<!--需要批量加载,设置batch-size="2"-->

<setname="students"

lazy="false"

inverse="true"

outer-join="false"

batch-size="2"

>

<keycolumn="team_id"></key>

<one-to-manyclass="Search.batch.immediately.Student"/>

</set>

</class>

</hibernate-mapping>

再次运行测试程序,结果如下:

Hibernate: select team0_.id as id2_, team0_.teamName as teamName2_ from team team0_

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id in (?, ?)

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id in (?, ?)

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id=?

可以看到,查询student只用了3条SQL(5/batch-size+1=3),

如果team对student是延迟加载的,会使什么效果呢,会是这样,我们加载team1时候,由于batch-size=2,所以加载team1就同时批量加载了team2,同理,加载team3时候也就加载了team4,最后再加载team5,  而这些数据都已经在hibernate session的缓存中了

修改team.hbm.xml 配置好lazy="true"

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

MappingfileautogeneratedbyMyEclipse-HibernateTools

-->

<hibernate-mappingpackage="Search.batch.immediately">

<classname="Team"table="team"lazy="false">

<idname="id"column="id">

<generatorclass="uuid.hex"></generator>

</id>

<propertyname="teamName"column="teamName"></property>

<!--需要批量加载,设置batch-size="2"-->

<setname="students"

lazy="true"

inverse="true"

outer-join="false"

batch-size="2"

>

<keycolumn="team_id"></key>

<one-to-manyclass="Search.batch.immediately.Student"/>

</set>

</class>

</hibernate-mapping>

运行测试程序,结果如下:

Hibernate: select team0_.id as id2_, team0_.teamName as teamName2_ from team team0_

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id in (?, ?)

team1:2

team2:1

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id in (?, ?)

team3:1

team4:1

Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.cardId as cardId1_0_, students0_.name as name1_0_, students0_.age as age1_0_, students0_.team_id as team5_1_0_ from student students0_ where students0_.team_id=?team5:1

其中可以砍到team2和team4的数据是批量加载来的,已经保存在缓存中,所以读取其students集合对象是,虽然是延迟加载,但不再会去发送SQ查询,而是再缓冲中查询

相关推荐