一的一方映射文件使用one-to-many
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate的DTD信息 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.crazyit.app.domain">
<!-- 映射Person持久化类-->
<class name="Person" table="person_inf">
<!-- 映射标识属性id -->
<id name="id" column="person_id">
<!-- 定义主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 用于映射普通属性 -->
<property name="name" type="string"/>
<property name="age" type="int"/>
<!-- 映射集合属性,集合元素是其他持久化实体
没有指定cascade属性,指定不控制关联关系 -->
<set name="addresses" inverse="true">
<!-- 指定关联的外键列 -->
<key column="person_id"/>
<!-- 用以映射到关联类属性 -->
<one-to-many class="Address"/>
</set>
</class>
</hibernate-mapping>
多的一方
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate的DTD信息 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.crazyit.app.domain">
<!-- Address持久化类-->
<class name="Address" table="address_inf">
<!-- 映射标识属性addressId -->
<id name="addressId" column="address_id">
<!-- 指定主键生成器策略 -->
<generator class="identity"/>
</id>
<!-- 映射普通属性addressdetail -->
<property name="addressDetail"/>
<!-- 必须指定列名为person_id,
与关联实体中key元素的column属性值相同 -->
<many-to-one name="person" class="Person"
column="person_id" not-null="true"/>
</class>
</hibernate-mapping>
package org.crazyit.app.domain;
public class Address
{
//标识属性
private int addressId;
//地址详细信息
private String addressDetail;
//记录关联实体的person属性
private Person person;
//无参数的构造器
public Address()
{
}
//初始化全部属性的构造器
public Address(String addressDetail)
{
this.addressDetail = addressDetail;
}
//addressId属性的setter和getter方法
public void setAddressId(int addressId)
{
this.addressId = addressId;
}
public int getAddressId()
{
return this.addressId;
}
//addressDetail属性的setter和getter方法
public void setAddressDetail(String addressDetail)
{
this.addressDetail = addressDetail;
}
public String getAddressDetail()
{
return this.addressDetail;
}
//person属性的setter和getter方法
public void setPerson(Person person)
{
this.person = person;
}
public Person getPerson()
{
return this.person;
}
}
package org.crazyit.app.domain;
import java.util.*;
public class Person
{
//标识属性
private Integer id;
//Person的name属性
private String name;
//保留Person的age属性
private int age;
//1-N关联关系,使用Set来保存关联实体
private Set<Address> addresses
= new HashSet<Address>();
//id属性的setter和getter方法
public void setId(Integer id)
{
this.id = id;
}
public Integer getId()
{
return this.id;
}
//name属性的setter和getter方法
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
//age属性的setter和getter方法
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
//addresses属性的setter和getter方法
public void setAddresses(Set<Address> addresses)
{
this.addresses = addresses;
}
public Set<Address> getAddresses()
{
return this.addresses;
}
}
测试
public class PersonManager
{
public static void main(String[] args)
{
PersonManager mgr = new PersonManager();
mgr.testPerson();
HibernateUtil.sessionFactory.close();
}
private void testPerson()
{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
//创建一个Person对象
Person p = new Person();
//设置Person的Name为Yeeku字符串
p.setName("Yeeku");
p.setAge(29);
//持久化Person对象(对应于插入主表记录)
session.save(p);
//创建一个瞬态的Address对象
Address a = new Address("广州天河");
//先设置Person和Address之间的关联关系
a.setPerson(p);
//再持久化Address对象(对应于插入从表记录)
session.persist(a);
//创建一个瞬态的Address对象
Address a2 = new Address("上海虹口");
//先设置Person和Address之间的关联关系
a2.setPerson(p);
//再持久化Address对象(对应于插入从表记录)
session.persist(a2);
tx.commit();
HibernateUtil.closeSession();
}
}