Hibernate的实体映射的主要任务就是实现数据库关系表与持久化类之间的映射,其关系如图:
双向映射一对一关联关系,通过唯一外键方式进行一对一关联映射,就是一个表的外键和另一个表的唯一主键对应形成一对一映射关系。
例如,以下例子,社团与社团负责人(社长),两者之间是一对一的关联关系:
持久化类association.java:
public class Association implements java.io.Serializable{
private Integer id;
private String name;
private Integer number;
private President president;
public Association {}
//省略各属性set/get方法
}持久化类president.java:
public class President implements java.io.Serializable{
private Integer id;
private String name;
private Integer age;
private Integer phone;
private String class;
private Association association;
public President {}
//省略各属性set/get方法
}association表与Association类的ORM映射文件Association.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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="entity.Association" table="association" catalog="stu_association"> <id name="ID" type="java.lang.Integer"> <column name="ID" /> <generator class="increment" /> </id> <property name="namme" type="java.lang.String"> <column name="namme" length="20" /> </property> <property name="number" type="java.lang.Integer"> <column name="number" /> </property>
<!-- 一对一的社团负责人 --> <many-to-one name="president" --|属性名字 class="entity.President" --|被关联类的名字 column="president" --|充当外键的字段名 lazy="false" --|是否立即加载 cascade="all" --|主控类的所有操作,对关联类也同样执行 unique="true"> --|唯一性约束,实现一对一关联的目的 </many-to-one> </class> </hibernate-mapping>
已经实现了Association到President的单向关联,如果要实现双向的关联,还需要在President.hbm.xml中进行配置。
President.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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="entity.Associationinformation" table="associationinformation" catalog="stu_association"> <id name="ID" type="java.lang.Integer"> <column name="ID" /> <generator class="increment" /> </id> <property name="name" type="java.lang.String"> <column name="name" length="20" /> </property> <property name="age" type="java.lang.Integer"> <column name="age" /> </property> <property name="phone" type="java.lang.Integer"> <column name="phone" /> </property> <property name="class" type="java.lang.String"> <column name="class" length="20" /> </property> <one-to-one name="association" --|属性名字 class="entity.Association" --|被关联的类的名字 property-ref="president"> --|指定关联类的属性名 </one-to-one> </class> </hibernate-mapping>
测试方法Test.java:
public void add(){
//社团
Association association = new Association();
association.setName("社团名字");
association.setNumber(100);
//负责人
President president = new President();
president.setName("张三");
president.setAge(20);
president.setPhone(12345678);
president.setClass("高一(1)班");
//设置关联
association.setPresident(president);
president.setAssociation(association);
session.save(association);
}
执行add()方法后,会向数据库中分别插入社团和负责人的属性,并且在社团的数据口表中,会多出一个president的字段,用于存放负责人的ID,从而实现了一对一的关联关系。
tips:
由于Association.hbm.xml中的cascade="all",所以当对Association进行了保存(save)操作后,与其一对一关联的President也会执行同样的操作
本文出自 “w1sw” 博客,请务必保留此出处http://cfdeng.blog.51cto.com/9873532/1754474
原文:http://cfdeng.blog.51cto.com/9873532/1754474