所谓立即加载,是在读取查询对象时,把和查询对象所有有关联的数据全部查出来(包括有关联的对象的关联对象),这种方式是效率最慢的,但好处是只需要一次数据库连接,就可以读取出全部数据,如果数据库连接资源紧张,可以考虑使用
我们有三个实体对象Student,Team,Cerificate
其中Student和Team是1对1的关系,采用主键关联的方式(Student的id和Certificate的id保持一致)
Team和Student是一对多的关系(反之为1对多)
在Mysql中运行以下脚本,建立数据库
CREATE TABLE certificate (
id varchar ( 100 ) NOT NULL default '' ,
description varchar ( 100 ) default '' ,
PRIMARY KEY (id)
);
CREATE TABLE student (
team_id varchar ( 100 ) default '' ,
id varchar ( 100 ) NOT NULL default '' ,
name varchar ( 20 ) default '' ,
cardId varchar ( 20 ) NOT NULL default '' ,
age int ( 11 ) default ' 0 ' ,
PRIMARY KEY (id)
);
CREATE TABLE team (
id varchar ( 100 ) NOT NULL default '' ,
teamName varchar ( 100 ) default '' ,
PRIMARY KEY (id)
);
INSERT INTO certificate VALUES ( ' 1 ' , ' 110108 ' );
INSERT INTO certificate VALUES ( ' 2 ' , ' 110109 ' );
INSERT INTO student VALUES ( ' 1 ' , ' 1 ' , ' tomclus ' , ' 2006m ' , 33 );
INSERT INTO student VALUES ( ' 2 ' , ' 2 ' , ' tom ' , ' 2007m ' , 22 );
INSERT INTO team VALUES ( ' 1 ' , ' team1 ' );
建立对应的Pojo对象
package Search.immediately;
public class Certificate ... {
private String id;
private String description;
private Student stu;
public Student getStu() ...{
return stu;
}
public void setStu(Student stu) ...{
this.stu = stu;
}
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
}
package Search.immediately;
import java.util.HashSet;
import java.util.Set;
public class Team ... {
private String id;
private Set students=new HashSet();
private String teamName;
private Set tests;
public Set getTests() ...{
return tests;
}
public void setTests(Set tests) ...{
this.tests = tests;
}
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public String getTeamName() ...{
return teamName;
}
public void setTeamName(String name) ...{
this.teamName = name;
}
public Set getStudents() ...{
return students;
}
public void setStudents(Set students) ...{
this.students = students;
}
}
package Search.immediately;
public class Certificate ... {
private String id;
private String description;
private Student stu;
public Student getStu() ...{
return stu;
}
public void setStu(Student stu) ...{
this.stu = stu;
}
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
}
建立对应的hbm文件
Student.hbm.xml
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping package ="Search.immediately" >
<!-- 设置非延迟加载 -->
< class name ="Student" table ="student" lazy ="false" >
< id name ="id" column ="id" unsaved-value ="null" >
< generator class ="uuid.hex" ></ generator >
</ id >
< property name ="cardId" column ="cardId" ></ property >
< property name ="name" column ="name" ></ property >
< property name ="age" column ="age" ></ property >
<!-- 为测试立即加载,关闭外连接 -->
< one-to-one name ="cer"
class ="Search.immediately.Certificate"
constrained ="true"
outer-join ="false"
cascade ="all" >
</ one-to-one >
<!-- hibernate会自动使用外连接获取学生所属的team,为测试立即加载,关闭外连接 -->
< many-to-one name ="team"
column ="team_id"
outer-join ="false"
class ="Search.immediately.Team" ></ many-to-one >
</ class >
</ hibernate-mapping >
Team.hbm.xml
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping package ="Search.immediately" >
< class name ="Team" table ="team" lazy ="false" >
< id name ="id" column ="id" >
< generator class ="uuid.hex" ></ generator >
</ id >
< property name ="teamName" column ="teamName" ></ property >
< set name ="students" lazy ="false" inverse ="true" outer-join ="false" >
< key column ="team_id" ></ key >
< one-to-many class ="Search.immediately.Student" />
</ set >
</ class >
</ hibernate-mapping >
Certificate.hbm .xml
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping package ="Search.immediately" >
< class name ="Certificate" table ="certificate" lazy ="false" >
<!-- 共享主键关联,在子方配置constrained=true -->
< id name ="id" column ="id" >
< generator class ="foreign" >
< param name ="property" > stu </ param >
</ generator >
</ id >
< property name ="description" column ="description" ></ property >
<!-- 为测试立即加载,关闭外连接 -->
< one-to-one name ="stu"
class ="Search.immediately.Student"
outer-join ="false"
constrained ="true" >
</ one-to-one >
</ class >
</ hibernate-mapping >
hibernate.cfg.xml
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" > root </ property >
< property name ="connection.url" >
jdbc:mysql://localhost:3306/hibernateconformity?characterEncoding=gb2312 & useUnicode=true
</ property >
< property name ="dialect" >
org.hibernate.dialect.MySQLDialect
</ property >
< property name ="myeclipse.connection.profile" > mysql </ property >
< property name ="connection.password" > 1234 </ property >
< property name ="connection.driver_class" >
com.mysql.jdbc.Driver
</ property >
< property name ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ property >
< property name ="hibernate.show_sql" > true </ property >
< property name ="current_session_context_class" > thread </ property >
< mapping resource ="Search/immediately/Certificate.hbm.xml" />
< mapping resource ="Search/immediately/Student.hbm.xml" />
< mapping resource ="Search/immediately/Team.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
测试程序
import java.io.File;
import java.io.FileInputStream;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test ... {
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"src/Search/immediately"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Student stu=(Student)session.get(Student.class, "1");
}
}
hibernate sql如下,我们开始分析
Hibernate: select student0_.id as id1_0_, student0_.cardId as cardId1_0_, student0_.name as name1_0_, student0_.age as age1_0_, student0_.team_id as team5_1_0_ from student student0_ where student0_.id=?
这条语句是因为我们调用了get方式,取id为1的student对象而产生的
Hibernate: select certificat0_.id as id0_0_, certificat0_.description as descript2_0_0_ from certificate certificat0_ where certificat0_.id=?
由于是立即加载,hibernate会自动根据取回的student对象来取得和他对应的Certificate
Hibernate: select team0_.id as id2_0_, team0_.teamName as teamName2_0_ from team team0_ where team0_.id=?
由于Student对象关联着其所属的team,这里取回team对象
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=?
team对象持有一个其所有学生的集合,所以,取回team对象后,继续查询student,把另一个(id为2)的studet对象取回,由于id为1的student已经在hibernate缓存中,所以,不会再进行一次数据库查询
Hibernate: select certificat0_.id as id0_0_, certificat0_.description as descript2_0_0_ from certificate certificat0_ where certificat0_.id=?
由于有了一个新取回的student对象,所以,立即加载其certificate对象
综上所述,简单的一个get操作,会引发5个SQL