Hibernate的基本映射
$映射文件结构
$主键生成器
$映射集合属性
一、List集合属性
1 .Person.java
public class Person implements Serializable
... {
//标识属性
private int id;
//名字属性
private String name;
//年龄属性
private int age;
//集合属性,学校
private List schools = new ArrayList();
Person() ...{}
public void setId(int id)
...{
this.id = id;
}
public int getId()
...{
return (this.id);
}
……
}
2 .Person.hbm.xml
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping
PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping >
<!-- 每个class元素对应一个持久化类 -->
< class name = " Person " >
<!-- id元素定义持久化类的标识属性 -->
< id name = " id " column = " personid " >
< generator class = " increment " />
</ id >
< property name = " name " />
< property name = " age " />
<!-- 映射集合属性schools,指定存放集合属性的表名 -->
< list name = " schools " table = " school " >
<!-- 集合属性和持久化类的关联外键 -->
< key column = " personid " not - null = " true " />
<!-- 集合属性的次序列 -->
< list - index column = " list_order " />
<!-- 集合属性的元素 -->
< element type = " String " column = " school_name " />
</ list >
</ class >
</ hibernate - mapping >
3 .(补充)建表SQL语句
create table Person(
personid integer not null auto_increment,
name varchar( 255 ),
age integer,
primary key (personid)
)
create table school (
personid integer not null ,
school_name varchar( 255 ),
list_order integer not null ,
primary key (personid, list_order)
)
4 .有了POJO及映射文件,该类就可以完成持久化访问
private void createAndStorePerson()
... {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Person prolove = new Person();
prolove.setAge(20);
prolvoe.setName("shuailee");
//创建集合属性
List l = new ArrayList();
l.add("小学");
l.add("中学");
//设置集合属性
prolove.setSchools(l);
//持久化Person实例,将状态保存到数据库
session.save(prolove);
tx.commit();
HibernateUtil.closeSession();
}
5 .(补充)HibernateUtil.java
package prolove;
import org.hibernate. * ;
import org.hibernate.cfg. * ;
public class HibernateUtil
... {
public static final SessionFactory sessionFactory;
static
...{
try
...{
//采用默认的hibernate.cfg.xml来启动一个Configuration的实例
Configuration configuration=new Configuration().configure();
//由Configuration的实例来创建一个SessionFactory实例
sessionFactory = configuration.buildSessionFactory();
}
catch (Throwable ex)
...{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
//ThreadLocal并不是线程本地化的实现,而是线程局部变量。也就是说每个使用该变量的线程都必须为
//该变量提供一个副本,每个线程改变该变量的值仅仅是改变该副本的值,而不会影响其他线程的该变量
//的值.
//ThreadLocal是隔离多个线程的数据共享,不存在多个线程之间共享资源,因此不再需要对线程同步
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException
...{
Session s = (Session) session.get();
//如果该线程还没有Session,则创建一个新的Session
if (s == null)
...{
s = sessionFactory.openSession();
//将获得的Session变量存储在ThreadLocal变量session里
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException ...{
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
二、Set集合属性
Set集合属性的映射与List非常相似,但因为Set是无序的,不可重复的集合。
因此Set元素无需使用index元素来指定集合元素的次序。
相同:SET同样需要外键列。用于持久化类和集合属性的关联。
1 .映射文件
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping
PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping >
< class name = " Person " >
< id name = " id " column = " personid " >
< generator class = " increment " />
</ id >
< property name = " name " />
< property name = " age " />
< set name = " schools " table = " school " >
< key column = " personid " not - null = " true " />
< element type = " string " column = " school_name " not - null = " true " />
</ set >
</ class >
</ hibernate - mapping >
三、bag元素映射
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping
PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping package = " prolove " >
< class name = " Person " >
< id name = " id " column = " personid " >
< generator class = " identity " />
</ id >
< property name = " name " />
< property name = " age " />
< bag name = " schools " table = " school " >
< key column = " personid " not - null = " true " />
< element type = " string " column = " school_name " />
</ bag >
</ class >
</ hibernate - mapping >
四、Map集合属性
所有集合属性:集合属性的声明只能使用接口。
1 .POJO
package prolove;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class Person implements Serializable
... {
private int id;
private String name;
private int age;
private Map scores = new HashMap();
Person() ...{}
public void setId(int id)
...{
this.id = id;
}
public int getId()
...{
return id;
}
public String getName()
...{
return name;
}
public void setName(String name)
...{
this.name = name;
}
public void setAge(int age)
...{
this.age = age;
}
public int getAge()
...{
return age;
}
public Map getScores()
...{
return scores;
}
public void setScores(Map scores)
...{
this.scores = scores;
}
}
2 .Person.hbm.xml
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping
PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping package = " prolove " >
< class name = " Person " >
< id name = " id " column = " personid " >
< generator class = " identity " />
</ id >
< property name = " name " />
< property name = " age " />
< map name = " scores " table = " score " >
<!-- 映射外键列 -->
< key column = " personid " not - null = " true " />
<!-- 映射 Map Key -->
< map - key column = " xueke " type = " string " />
<!-- 映射 Map Value -->
< element column = " fenshu " type = " float " />
</ map >
</ class >
</ hibernate - mapping >
五、集合性能的对比
延迟加载策略。lazy = " fales " 取消延迟加载
* 有序集合:集合里的元素可以根据key或index访问
* 无序集合:集合里的元素只能遍历
inverse = " true "
$映射文件结构
$主键生成器
$映射集合属性
一、List集合属性
1 .Person.java
public class Person implements Serializable
... {
//标识属性
private int id;
//名字属性
private String name;
//年龄属性
private int age;
//集合属性,学校
private List schools = new ArrayList();
Person() ...{}
public void setId(int id)
...{
this.id = id;
}
public int getId()
...{
return (this.id);
}
……
}
2 .Person.hbm.xml
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping
PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping >
<!-- 每个class元素对应一个持久化类 -->
< class name = " Person " >
<!-- id元素定义持久化类的标识属性 -->
< id name = " id " column = " personid " >
< generator class = " increment " />
</ id >
< property name = " name " />
< property name = " age " />
<!-- 映射集合属性schools,指定存放集合属性的表名 -->
< list name = " schools " table = " school " >
<!-- 集合属性和持久化类的关联外键 -->
< key column = " personid " not - null = " true " />
<!-- 集合属性的次序列 -->
< list - index column = " list_order " />
<!-- 集合属性的元素 -->
< element type = " String " column = " school_name " />
</ list >
</ class >
</ hibernate - mapping >
3 .(补充)建表SQL语句
create table Person(
personid integer not null auto_increment,
name varchar( 255 ),
age integer,
primary key (personid)
)
create table school (
personid integer not null ,
school_name varchar( 255 ),
list_order integer not null ,
primary key (personid, list_order)
)
4 .有了POJO及映射文件,该类就可以完成持久化访问
private void createAndStorePerson()
... {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Person prolove = new Person();
prolove.setAge(20);
prolvoe.setName("shuailee");
//创建集合属性
List l = new ArrayList();
l.add("小学");
l.add("中学");
//设置集合属性
prolove.setSchools(l);
//持久化Person实例,将状态保存到数据库
session.save(prolove);
tx.commit();
HibernateUtil.closeSession();
}
5 .(补充)HibernateUtil.java
package prolove;
import org.hibernate. * ;
import org.hibernate.cfg. * ;
public class HibernateUtil
... {
public static final SessionFactory sessionFactory;
static
...{
try
...{
//采用默认的hibernate.cfg.xml来启动一个Configuration的实例
Configuration configuration=new Configuration().configure();
//由Configuration的实例来创建一个SessionFactory实例
sessionFactory = configuration.buildSessionFactory();
}
catch (Throwable ex)
...{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
//ThreadLocal并不是线程本地化的实现,而是线程局部变量。也就是说每个使用该变量的线程都必须为
//该变量提供一个副本,每个线程改变该变量的值仅仅是改变该副本的值,而不会影响其他线程的该变量
//的值.
//ThreadLocal是隔离多个线程的数据共享,不存在多个线程之间共享资源,因此不再需要对线程同步
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException
...{
Session s = (Session) session.get();
//如果该线程还没有Session,则创建一个新的Session
if (s == null)
...{
s = sessionFactory.openSession();
//将获得的Session变量存储在ThreadLocal变量session里
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException ...{
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
二、Set集合属性
Set集合属性的映射与List非常相似,但因为Set是无序的,不可重复的集合。
因此Set元素无需使用index元素来指定集合元素的次序。
相同:SET同样需要外键列。用于持久化类和集合属性的关联。
1 .映射文件
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping
PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping >
< class name = " Person " >
< id name = " id " column = " personid " >
< generator class = " increment " />
</ id >
< property name = " name " />
< property name = " age " />
< set name = " schools " table = " school " >
< key column = " personid " not - null = " true " />
< element type = " string " column = " school_name " not - null = " true " />
</ set >
</ class >
</ hibernate - mapping >
三、bag元素映射
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping
PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping package = " prolove " >
< class name = " Person " >
< id name = " id " column = " personid " >
< generator class = " identity " />
</ id >
< property name = " name " />
< property name = " age " />
< bag name = " schools " table = " school " >
< key column = " personid " not - null = " true " />
< element type = " string " column = " school_name " />
</ bag >
</ class >
</ hibernate - mapping >
四、Map集合属性
所有集合属性:集合属性的声明只能使用接口。
1 .POJO
package prolove;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class Person implements Serializable
... {
private int id;
private String name;
private int age;
private Map scores = new HashMap();
Person() ...{}
public void setId(int id)
...{
this.id = id;
}
public int getId()
...{
return id;
}
public String getName()
...{
return name;
}
public void setName(String name)
...{
this.name = name;
}
public void setAge(int age)
...{
this.age = age;
}
public int getAge()
...{
return age;
}
public Map getScores()
...{
return scores;
}
public void setScores(Map scores)
...{
this.scores = scores;
}
}
2 .Person.hbm.xml
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping
PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping package = " prolove " >
< class name = " Person " >
< id name = " id " column = " personid " >
< generator class = " identity " />
</ id >
< property name = " name " />
< property name = " age " />
< map name = " scores " table = " score " >
<!-- 映射外键列 -->
< key column = " personid " not - null = " true " />
<!-- 映射 Map Key -->
< map - key column = " xueke " type = " string " />
<!-- 映射 Map Value -->
< element column = " fenshu " type = " float " />
</ map >
</ class >
</ hibernate - mapping >
五、集合性能的对比
延迟加载策略。lazy = " fales " 取消延迟加载
* 有序集合:集合里的元素可以根据key或index访问
* 无序集合:集合里的元素只能遍历
inverse = " true "