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

土豆的小秘密 2008-05-22

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

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

首先建立测试数据库

CREATETABLEcertificate(

idvarchar(100)NOTNULLdefault'',

descriptionvarchar(100)default'',

PRIMARYKEY(id)

);

CREATETABLEstudent(

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);

INSERTINTOteamVALUES

('5','team5'),

('4','team4'),

('3','team3'),

('2','team2'),

('1','team1');

INSERTINTOcertificateVALUES

('1','card1'),

('2','card2'),

('3','card3'),

('4','card4'),

('5','card5'),

('6','card6');

建立POJO对象

packageSearch.immediately;

publicclassCertificate............{

privateStringid;

privateStringdescription;

privateStudentstu;

publicStudentgetStu()............{

returnstu;

}

publicvoidsetStu(Studentstu)............{

this.stu=stu;

}

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

returndescription;

}

publicvoidsetDescription(Stringdescription)............{

this.description=description;

}

publicStringgetId()............{

returnid;

}

publicvoidsetId(Stringid)............{

this.id=id;

}

}

packageSearch.immediately;

importjava.util.HashSet;

importjava.util.Set;

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

privateStringid;

privateSetstudents=newHashSet();

privateStringteamName;

privateSettests;

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

returntests;

}

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

this.tests=tests;

}

publicStringgetId()............{

returnid;

}

publicvoidsetId(Stringid)............{

this.id=id;

}

publicStringgetTeamName()............{

returnteamName;

}

publicvoidsetTeamName(Stringname)............{

this.teamName=name;

}

publicSetgetStudents()............{

returnstudents;

}

publicvoidsetStudents(Setstudents)............{

this.students=students;

}

}

packageSearch.immediately;

publicclassCertificate............{

privateStringid;

privateStringdescription;

privateStudentstu;

publicStudentgetStu()............{

returnstu;

}

publicvoidsetStu(Studentstu)............{

this.stu=stu;

}

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

returndescription;

}

publicvoidsetDescription(Stringdescription)............{

this.description=description;

}

publicStringgetId()............{

returnid;

}

publicvoidsetId(Stringid)............{

this.id=id;

}

}

HBM文件

student.hbm.xml

<?xmlversion="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="Student"table="student"lazy="false">

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

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

</id>

<propertyname="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

<?xmlversion="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

<?xmlversion="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>

测试代码:

packageSearch.batch.immediately;

importjava.io.File;

importjava.util.List;

importorg.hibernate.Query;

importorg.hibernate.Session;

importorg.hibernate.SessionFactory;

importorg.hibernate.cfg.Configuration;

publicclassTest...{

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");

ListresultList=query.list();

}

运行,结果如下:

Hibernate:selectteam0_.idasid2_,team0_.teamNameasteamName2_fromteamteam0_

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_id=

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_id=?

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_id=?

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_id=?

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_id=?

有5条查询student的SQL(本身有6个学生,但有两个学生属于一个team)

可以看到,这是典型的立即加在,取得team对象后,把team所包含的student取出,如果有100个学生,则会执行100个SQL,这是很没有效率的

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

<?xmlversion="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:selectteam0_.idasid2_,team0_.teamNameasteamName2_fromteamteam0_

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_idin(?,?)

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,

students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_idin(?,?)

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_id=?

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

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

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

<?xmlversion="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:selectteam0_.idasid2_,team0_.teamNameasteamName2_fromteamteam0_

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_idin(?,?)

team1:2

team2:1

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_idin(?,?)

team3:1

team4:1

Hibernate:selectstudents0_.team_idasteam5_1_,students0_.idasid1_,students0_.idasid1_0_,students0_.cardIdascardId1_0_,students0_.nameasname1_0_,students0_.ageasage1_0_,students0_.team_idasteam5_1_0_fromstudentstudents0_wherestudents0_.team_id=?

team5:1

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

相关推荐