环境: eclipse+myeclipse+mysql.
(1) 建表脚本
--删除表
drop table if exists person ;
drop table if exists email ;
--新建表
create table person
(
id int AUTO_INCREMENT not null primary key ,
name varchar(20)
) ;
create table email
(
id int not null ,
address varchar(50) not null ,
foreign key (id) references person(id) on delete cascade
);
--提交
commit ;
(2) Person.java的编写.
package org.hibernate.listmapping.vo;
import java.util.HashSet;
import java.util.Set;
/**
* Person generated by MyEclipse Persistence Tools
*/
public class Person implements java.io.Serializable {
private int id ;
private String name ;
private Set email = new HashSet() ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getEmail() {
return email;
}
public void setEmail(Set email) {
this.email = email;
}
}
(3) Person.hbm.xml的编写.
<?xml version="1.0" encoding="utf-8"?>
<!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 Persistence Tools
-->
<hibernate-mapping>
<class name="org.hibernate.listmapping.vo.Person" table="person">
<id name="id" type="java.lang.Integer">
<column name="id">
</column>
<generator class="native">
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name"></column>
</property>
<set name="email" table="email" lazy="false">
<key column="id"></key>
<element type="java.lang.String" >
<column name="address"></column>
</element>
</set>
</class>
</hibernate-mapping>
(4) hibernate.cfg.xml配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!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/hibernatetable
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">mysqladmin</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="org/hibernate/listmapping/vo/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
(5)Insert测试类
package org.hibernate.listmapping.dao;
import org.hibernate.HibernateSessionFactory;
import org.hibernate.Session;
import org.hibernate.listmapping.vo.Person;
public class PersonDao {
public void insert(Person p)
{
Session session = HibernateSessionFactory.getSession() ;
session.save(p) ;
session.beginTransaction().commit() ;
session.close() ;
}//end method insert
public static void main(String[] args)
{
PersonDao pd = new PersonDao() ;
Person p = new Person() ;
//插入两个相同的email,在实际上数据库中只有一个,
// set的特性.
p.getEmail().add("123@163.com") ;
p.getEmail().add("123@163.com") ;
p.getEmail().add("124@163.com") ;
p.getEmail().add("125@163.com") ;
p.setName("zhangsan") ;
pd.insert(p) ;
}
}
(6)lazy 延迟加载.
将此处的<set name="email" table="email" lazy="false">改成
<set name="email" table="email" lazy="true">,就是Hibernate的延迟加载(懒加载机制了.
延迟加载测试类.
package org.hibernate.setDemo.dao;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.SimpleExpression;
import org.hibernate.setDemo.vo.Person;
public class PersonDao {
private Session session ;
public PersonDao()
{
session = new Configuration().configure().buildSessionFactory()
.openSession() ;
}
public void insert(Person p)
{
session.save(p) ;
session.beginTransaction().commit() ;
session.close() ;
}
public void update(Person p)
{
session.update(p) ;
session.beginTransaction().commit() ;
session.close() ;
}
public Person findById(int id)
{
Person p ;
Criteria c = session.createCriteria(Person.class) ;
SimpleExpression se = Expression.eq("id", id) ;
c.add(se) ;
List l = c.list() ;
if(l.size()>0)
{
p = (Person) l.get(0) ;
return p ;
}
return null ;
}
public static void main(String []args)
{
PersonDao pd = new PersonDao() ;
Person p = pd.findById(1) ;
System.out.println(p.getName()) ;
Set s = p.getEmail() ;
Iterator i = s.iterator() ;
while(i.hasNext())
{
String address = (String)i.next() ;
System.out.println(address) ;
}
}
}
当我们的 Lazy = false时,运行上面的测试类控制台输出如下:
Hibernate: select this_.id as id0_0_, this_.name as name0_0_ from person this_ where this_.id=?
Hibernate: select email0_.id as id0_, email0_.address as address0_ from email email0_ where email0_.id=?
zhangsan
126@163.com
125@163.com
123@163.com
124@163.com
127@163.com
分析:我们可以看到,在查这个人的时候,附带的把这个人的地址都查出来放入内存中了.
当我们的 Lazy = true 时,运行上面的测试类控制台输出如下:
Hibernate: select this_.id as id0_0_, this_.name as name0_0_ from person this_ where this_.id=?
zhangsan
Hibernate: select email0_.id as id0_, email0_.address as address0_ from email email0_ where email0_.id=?
126@163.com
125@163.com
123@163.com
124@163.com
127@163.com
分析:我们可以很清楚的看到,是在我们要这个人的email地址的时候,才到数据库中去查,而不是事先去查.