一、双向1 - N的关系映射对于1 - N的关联,Hibernate推荐使用双向关联,但不用1的一端来控制关联关系,而使用N的一端来控制关联关系。N的一端直接访问关联类属性;1的一端增加集合属性的访问。看下面2个POJO public class Person implements Serializable ... { private int personid; priavte String name; private int age; private Set addresses = new HashSet(); ……} public class Address implements Serializable ... { private int addressid; private String addressdetail; private Person person; public Address() ...{} public Address(Sring addressdetail) ...{ this.addressdetail = addressdetail; }} 1 .无连接表的双向1 - N关联两个持久化类的配置文件都需要指定外键列的列名 <? xml version = " 1.0 " ?> <! DOCTYPE hibernate - mapping PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN " " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " > < hibernate - mapping package = " prolove " > < class name = " Address " > < id name = " addressid " > < generator class = " identity " /> </ id > < property name = " addressdetail " /> <!-- 增加many - to - one映射关联属性,column属性指定外键列名 --> < many - to - one name = " person " column = " personid " /> </ class > < class name = " Person " > < id name = " personid " > < generator class = " identity " /> </ id > < property name = " name " /> < property name = " age " /> < set name = " addresss " > < key column = " personid " /> < one - to - many class = " Address " /> </ set > </ class > </ hibernate - mapping > 2 .有连接表的双向1 - N关联两边确定连接表的table属性值应该相同,且不能省略。 <? xml version = " 1.0 " ?> <! DOCTYPE hibernate - mapping PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN " " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " > < hibernate - mapping package = " prolove " > < class name = " Address " > < id name = " addressid " > < generator class = " identity " /> </ id > < property name = " addressdetail " /> <!-- 显式使用join元素确定连接表 --> < join table = " PersonAddress " inverse = " true " optional = " true " > < key column = " addressid " /> < many - to - one name = " person " column = " personid " not - null = " true " /> </ join > </ class > < class name = " Person " > < id name = " personid " > < generator class = " identity " /> </ id > < property name = " name " /> < property name = " age " /> <!-- 映射集合属性,关联到持久化类,注明table属性 --> < set name = " addresses " table = " PersonAddress " > < key column = " personid " /> <!-- one - to - many元素映射关联属性,为保证为1,增加unique = " true " --> < one - to - many column = " addressid " unique = " true " class = " Address " /> </ set > </ class > </ hibernate - mapping > 二、双向N - N关联 public class Person ... { private int personid; private String name; private int age; private Set addresses = new HashSet(); ……} public class Address ... { private int addressid; private String addressdetail; private Set persons = new HashSet(); public Address() ...{} public Address(String addressdetail) ...{ this.addressdetail = addressdetail; }} <? xml version = " 1.0 " ?> <! DOCTYPE hibernate - mapping PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN " " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " > < hibernate - mapping package = " prolove " > < class name = " Address " > < id name = " addressid " > < generator class = " identity " /> </ id > < property name = " addressdetail " /> <!-- 映射集合属性,使用指定连接表 --> < set name = " persons " inverse = " true " table = " jointable " > <!-- 指定本持久化类在连接表中的外键列名 --> < key column = " addressid " /> <!-- 映射多对多关联类 --> < many - to - many column = " personid " class = " Person " /> </ set > </ class > < class name = " Person " > < id name = " personid " > < generator class = " identity " /> </ id > < property name = " name " /> < property name = " age " /> <!-- 映射集合属性,关联到持久化类,注明table属性 --> < set name = " addresses " table = " jointable " > < key column = " personid " /> < many - to - many column = " addressid " class = " Address " /> </ set > </ class > </ hibernate - mapping > 三、双向1 - 1关联 public class Person ... { //基本属性 private int personid; private String name; private int age; //关联属性 private Address address; ……} public class Address implements Serializable ... { private int addressid; private String addressdetail; private Person person; public Address() ...{} public Address(String addressdetail) ...{ this.addressdetail = addressdetail; } ……} 1 .基于外键的双向1 - 1关联 <? xml version = " 1.0 " ?> <! DOCTYPE hibernate - mapping PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN " " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " > < hibernate - mapping package = " prolove " > < class name = " Person " > < id name = " personid " > < generator class = " identity " /> </ id > < property name = " name " /> < property name = " age " /> <!-- one - to - one元素映射关联属性,外键列在对方的表内person - ref 指向关联类的本身的属性。即在address属性所属的Address类 --> < one - to - one name = " address " property - ref = " person " /> </ class > < class name = " Address " > < id name = " addressid " > < generator class = " identity " /> </ id > < property name = " addressdetail " /> <!-- many - to - one元素映射关联属性,unique = " true " 确定为1 - 1 --> < many - to - one name = " person " unique = " true " /> </ class > </ hibernate - mapping > 2 .基于主键的双向1 - 1关联 <? xml version = " 1.0 " ?> <! DOCTYPE hibernate - mapping PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN " " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " > < hibernate - mapping package = " prolove " > < class name = " Person " > < id name = " personid " > < generator class = " identity " /> </ id > < property name = " name " /> < property name = " age " /> <!-- one - to - one元素映射关联属性 --> < one - to - one name = " address " /> </ class > < class name = " Address " > < id name = " addressid " > < generator class = " foreign " > <!-- 指定根据主键生成的关联属性 --> < param name = " property " > person </ param > </ generator > </ id > < property name = " addressdetail " /> <!-- 用于映射关联属性,增加constrained = " true " 表明主键根据关联属性生成 --> < one - to - one name = " person " constrained = " true " /> </ class > </ hibernate - mapping > 3 .有连接表的双向1 - 1关联本情形相当罕见,通常不推荐使用这种策略。 <? xml version = " 1.0 " ?> <! DOCTYPE hibernate - mapping PUBLIC " -//Hibernate/Hibernate Mapping DTD 3.0//EN " " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " > < hibernate - mapping package = " prolove " > < class name = " Person " > < id name = " personid " > < generator class = " identity " /> </ id > < property name = " name " /> < property name = " age " /> <!-- 显式使用join元素指定连接表关联 --> < join table = " PersonAddress " optional = " true " > <!-- key元素映射外键列的列名 --> < key column = " personid " unique = " true " /> <!-- 映射关联属性 --> < many - to - one name = " address " column = " addressid " not - null = " true " unique = " true " /> </ join > </ class > <!-- 映射持久化类Address --> < class name = " Address " > <!-- 映射标识属性 --> < id name = " addressid " > <!-- 指定主键生成器策略 --> < generator class = " identity " /> </ id > < property name = " addressdetail " /> <!-- 显式使用join元素指定连接表关联 --> < join table = " PersonAddress " inverse = " true " optional = " true " > < key column = " addressid " unique = " true " /> < many - ton - one name = " person " column = " personid " not - null = " true " unique = " true " /> </ join > </ class > </ hibernate - mapping >