组件映射有三种情况:
- 组件类作为持久化类的单个属性
- 组件类作为持久化类的集合属性
- 组件类作为持久化类的对象标识符属性
组件类作为持久化类的单个属性
实体类
package com.java.hibernate;
/**
* 地址类
* 值类型,它没有对象标识符属性。
* 只能隶属于一个持久化类的实例,它的数据也是被嵌入到所隶属的持久化实例对应的数据库表的记录的。
* @author itachi
*
*/
public class Address {
private String province; //省份
private String city; //城市
private String detail; //详细地址
private String zipCode; //邮编
private String telephone; //固定电话
private String mobilePhone; //移动电话
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getMobilePhone() {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
}
package com.java.hibernate;
public class Customer {
private Long id; //对象标识符(OID)
private String loginname; //登录名
private String password; //密码
private String email; //邮箱
private Address homeAddress; //联系地址
private Address deliverAddress; //送货地址
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Address getDeliverAddress() {
return deliverAddress;
}
public void setDeliverAddress(Address deliverAddress) {
this.deliverAddress = deliverAddress;
}
}
映射文件
<?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.java.hibernate.Customer" table="t_customer">
<id name="id" type="long">
<generator class="native"></generator>
</id>
<property name="loginname" not-null="true"></property>
<property name="password" not-null="true"></property>
<property name="email"></property>
<!-- 用component元素映射组件类属性homeAddress -->
<component name="homeAddress" class="com.java.hibernate.Address">
<!-- 用property映射组件类的每个属性 -->
<property name="province" column="home_province"></property>
<property name="city" column="home_city"></property>
<property name="detail" column="home_detail"></property>
<property name="zipCode" column="home_zipcode"></property>
<property name="telephone" column="home_telephone"></property>
<property name="mobilePhone" column="home_mobilephone"></property>
</component>
<!-- 用component元素映射组件类属性deliverAddress -->
<component name="deliverAddress" class="com.java.hibernate.Address">
<property name="province" column="deliver_province"></property>
<property name="city" column="deliver_city"></property>
<property name="detail" column="deliver_detail"></property>
<property name="zipCode" column="deliver_zipCode"></property>
<property name="telephone" column="deliver_telephone"></property>
<property name="mobilePhone" column="deliver_mobilephone"></property>
</component>
</class>
</hibernate-mapping>
测试类
package com.java.hibernate;
import junit.framework.TestCase;
import org.hibernate.Session;
public class Test extends TestCase {
public void test() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Customer customer = new Customer();
customer.setLoginname("莫圣急");
customer.setPassword("123******");
customer.setEmail("msj@***.***");
Address homeAddress = new Address();
homeAddress.setProvince("北京市");
homeAddress.setCity("海淀区");
homeAddress.setDetail("xx路xx小区xx号");
homeAddress.setZipCode("10020");
homeAddress.setTelephone("010-6656****");
homeAddress.setMobilePhone("1369911****");
customer.setHomeAddress(homeAddress);
Address deliverAddress = new Address();
deliverAddress.setProvince("福建省");
deliverAddress.setCity("厦门市");
deliverAddress.setDetail("yy路yy小区yy号");
deliverAddress.setZipCode("350000");
deliverAddress.setTelephone("0592-6656****");
deliverAddress.setMobilePhone("1399911****");
customer.setDeliverAddress(deliverAddress);
session.save(customer);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
组件类作为持久化类的集合属性
实体类
package com.java.hibernate;
import java.util.Date;
import java.util.List;
public class Album {
private Long id; //对象标识符
private String title; //相册名
private String description; //描述
private Date creationTime; //创建时间
private List<Photo> photos; //照片集
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreationTime() {
return creationTime;
}
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}
public List<Photo> getPhotos() {
return photos;
}
public void setPhotos(List<Photo> photos) {
this.photos = photos;
}
}
package com.java.hibernate;
public class Photo {
private String name; //照片名
private String filePath; //照片存放路径
private String description; //描述
private int width; //宽度
private int height; //高度
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
映射文件
<?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 package="com.java.hibernate">
<class name="Album" table="t_album">
<id name="id" type="long">
<generator class="native"></generator>
</id>
<property name="title"></property>
<property name="description"></property>
<property name="creationTime" column="createion_time"></property>
<!-- 用bag来映射List集合属性 -->
<bag name="photos" table="t_photo">
<!-- key元素:指定集合属性对应的表的外键列 -->
<key column="album_id" not-null="true"></key>
<!-- composite-element元素:映射集合里的元素
class属性:指定集合里的元素的类类型
-->
<composite-element class="Photo">
<property name="name"></property>
<property name="filePath" column="file_path"></property>
<property name="description"></property>
<property name="width"></property>
<property name="height"></property>
</composite-element>
</bag>
</class>
</hibernate-mapping>
测试类
package com.java.hibernate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import junit.framework.TestCase;
import org.hibernate.Session;
public class Test extends TestCase {
public void test() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Album album = new Album();
album.setTitle("成长记");
album.setDescription("记录生活点滴");
album.setCreationTime(new Date());
List<Photo> photos = new ArrayList<Photo>();
Photo photo = new Photo();
photo.setName("photo1");
photo.setDescription("desc00001");
photo.setFilePath("d:\20009001.jpg");
photo.setHeight(768);
photo.setWidth(1024);
photos.add(photo);
Photo photo2 = new Photo();
photo2.setName("photo2");
photo2.setDescription("desc00002");
photo2.setFilePath("d:\20009002.jpg");
photo2.setHeight(1400);
photo2.setWidth(1050);
photos.add(photo2);
album.setPhotos(photos);
session.save(album);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
组件类作为持久化类的对象标识符属性
实体类
package com.java.hibernate;
/**
* 用户持久化类(实体类)
* @author itachi
*
*/
public class User {
private UserOid id; //对象标识符
private String description; //描述
public User() {}
public UserOid getId() {
return id;
}
public void setId(UserOid id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.java.hibernate;
import java.io.Serializable;
/**
* 用户类的对象标识符类
* 必须实现Serializable和重写hashCode()和equals()方法
* @author itachi
*
*/
public class UserOid implements Serializable{
private static final long serialVersionUID = -9079299317576122433L;
private String firstName; //名
private String lastName; //姓
public UserOid() {}
//用fristName、lastName重写hashCode方法
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
//用fristName、lastName重写equals方法
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
UserOid other = (UserOid) obj;
if (firstName == null) {
if (other.firstName != null) return false;
} else if (!firstName.equals(other.firstName)) return false;
if (lastName == null) {
if (other.lastName != null) return false;
} else if (!lastName.equals(other.lastName)) return false;
return true;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
映射文件
<?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.java.hibernate.User" table="t_user">
<!--
composite-id元素映射复合对象标识符
name属性:指定属性名
class属性:指定对应的此属性的类类型
-->
<composite-id name="id" class="com.java.hibernate.UserOid">
<!-- key-property元素映射组件类的的每个属性 -->
<key-property name="firstName" column="first_name"></key-property>
<key-property name="lastName" column="last_name"></key-property>
</composite-id>
<property name="description"></property>
</class>
</hibernate-mapping>
测试类
package com.java.hibernate;
import junit.framework.TestCase;
import org.hibernate.Session;
public class Test extends TestCase {
public void test() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
User user = new User();
user.setDescription("这是复合OID的测试");
UserOid id = new UserOid();
id.setFirstName("升级");
id.setLastName("章");
user.setId(id);
//利用Session实例进行持久化操作
session.save(user);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}