我们会把一个类中的其他有关的信息封装到一个实体bean中,通过组合的关系进行映射
如下 clients表
id
name
linkname
phone
province
city
street
zipcode
Clients 与 Address是组合关系
Clients ------------------------> Address(把以下几个字段抽象为一个bean)
id province
name city
linkname street
phone zipcode
Address.java
package com.xiu.hibernate.other;
import java.io.Serializable;
public class Address implements Serializable{
private static final long serialVersionUID = 1L;
private String province;
private String city;
private String street;
private String zipcode;
private Clients clients;
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 getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public Clients getClients() {
return clients;
}
public void setClients(Clients clients) {
this.clients = clients;
}
}
Clients.java
package com.xiu.hibernate.other;
import java.io.Serializable;
public class Clients implements Serializable{
private Integer id;
private String name;
private String linkname;
private String phone;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLinkname() {
return linkname;
}
public void setLinkname(String linkname) {
this.linkname = linkname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Clients.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" >
<hibernate-mapping package="com.xiu.hibernate.other">
<class name="Clients" table="clients">
<id name="id" column="ID" type="integer">
<generator class="identity" />
</id>
<property name="name" column="name" type="string" />
<property name="linkname" column="linkname" type="string" />
<property name="phone" column="phone" type="string" />
<component name="address"
class="com.xiu.hibernate.other.Address">
<parent name="clients" /><!--Address引用了clients -->
<!-- 以下是进行组件类属性与表字段之间的持久化映射 -->
<property name="province" column="province" type="string" />
<property name="city" column="city" type="string" />
<property name="street" column="street" type="string" />
<property name="zipcode" column="zipcode" type="string" />
</component>
</class>
</hibernate-mapping>
测试
package com.xiu.hibernate.other;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class TestCompents {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateSessionFactory
.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.begin();
Address address = new Address();
address.setCity("武汉");
address.setProvince("湖北");
address.setStreet("武昌区");
address.setZipcode("430205");
Clients clients = new Clients();
clients.setAddress(address);
clients.setLinkname("朱先生");
clients.setName("hello");
clients.setPhone("119");
session.save(clients);
tx.commit();
}
}