Hibernate 学习笔记第三篇
Hibernate 的高级映射之组件映射
组件映射:示例一
一个商品订购系统,一个Custmer 可能有联系地址,还有送货地址,
那么我们把地址分出一个实体类
public class Address {
private String province;
private String city;
private String detail;
private String phone;
getters();
setters();
}
用户信息是一个执久化类:
public class Customer {
private int id;
private String loginName;
private String pwd;
/***联系地址*/
private Address homeAddress;
/***送货地址*/
private Address deliverAddress;
setters();
getters();
}
Customer.cfg.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">
<hibernate-mapping>
<class name="com.wlzx.modle.Customer" table="customer">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="loginName" />
<property name="pwd" />
<!-- 组件映射 -->
<component name="homeAddress" class="com.wlzx.modle.Address">
<property name="province" column="home_province"></property>
<property name="city" column="home_city"></property>
<property name="detail" column="home_detail" />
<property name="phone" column="home_phone"></property>
</component>
<component name="deliverAddress" class="com.wlzx.modle.Address">
<property name="province" column="deliver_province"></property>
<property name="city" column="deliver_city"></property>
<property name="detail" column="deliver_detail" />
<property name="phone" column="deliver_phone"></property>
</component>
</class>
</hibernate-mapping>
映射结果: 映射成一个表
create table customer (
id integer not null auto_increment,
loginName varchar(255),
pwd varchar(255),
home_province varchar(255),
home_city varchar(255),
home_detail varchar(255),
home_phone varchar(255),
deliver_province varchar(255),
deliver_city varchar(255),
deliver_detail varchar(255),
deliver_phone varchar(255),
primary key (id)
);
组件映射:示例二
一个相册有多个相片:可以把相册定义成 个持久化类,而相片定义成一个组件。
/****Album.java 相册**/
public class Album {
private int id;
private String title;
private String desc;
private List<Phone> phones;
getters();
setters();
}
/***phone.java 照片****/
public class Phone {
private String name;
private String filePath;
private String desc;
private int width;
private int heigth;
getters();
setters();
}
Album.cfg.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">
<hibernate-mapping>
<class name="com.wlzx.modle.Album" table="album">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="title" />
<property name="desc" />
<!-- 组件映射 -->
<bag name="phones" table="phone">
<key column="album_id"></key>
<composite-element class="com.wlzx.modle.Phone">
<property name="name"></property>
<property name="filePath" column="file_path"></property>
<property name="desc"></property>
<property name="width"></property>
<property name="heigth"></property>
</composite-element>
</bag>
</class>
</hibernate-mapping>
映射结果:映射成两个表
-- 相册表
create table album (
id integer not null auto_increment,
title varchar(255),
desc varchar(255),
primary key (id)
);
-- 照片表
create table phone (
album_id integer not null,
name varchar(255),
file_path varchar(255),
desc varchar(255),
width integer,
heigth integer
);