Hibernate关联关系映射目录
│
├─单向关联
│ ├─ 一对一外键单向关联
│ ├─ 一对一主键单向关联
│ ├─ 一对一连接表单向关联
│ ├─ 一对多外键单向关联
│ ├─ 一对多连接表单向关联
│ ├─ 多对一外键单向关联
│ ├─ 多对一连接表单向关联
│ └─ 多对多单向关联
└─双向关联
├─ 一对一外键双向关联
├─ 一对一主键双向关联
├─ 一对一连接表双向关联
├─ 一对多外键双向关联
├─ 一对多连接表双向关联
└─ 多对多双向关联
Hibernate 一对一外键单向关联
<hibernate-mapping>
<class name="com.lavasoft.dx._1_1_fk.Person11fk" table="PERSON_11fk">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--用来映射关联PO column是Address在该表中的外键列名,增加unique变成“1-1”-->
<many-to-one name="address11fk" column="addressId" unique="true"/>
</class>
</hibernate-mapping>
Hibernate
多对多单向关联
<hibernate-mapping>
<class name="com.lavasoft.dx._n_n.Personnn" table="PERSON_nn">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--映射集合属性,join_1ntab是连接表表名-->
<set name="addresses"
table="join_nn"
>
<!--“column="personid"”确定PERSON_1ntab表关联到连接表的外键列名-->
<key column="personid"/>
<!--“column="addressid"”关联PERSON_1ntab表的Address1ntab对象的id在连接表中的列名-->
<many-to-many
column="addressid"
class="com.lavasoft.dx._n_n.Addressnn"/>
</set>
</class>
</hibernate-mapping>
Hibernate 一对一外键双向关联
<?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="entity.Person" table="person">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="24" not-null="true">
<comment>姓名</comment>
</column>
</property>
<one-to-one name="address" cascade="all" />
</class>
</hibernate-mapping>
<?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="entity.Address" table="address" catalog="testdb">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="detail" type="java.lang.String">
<column name="detail" length="120" not-null="true">
<comment>详细地址</comment>
</column>
</property>
<many-to-one name="person" class="entity.Person"
fetch="select" unique="true">
<column name="personid">
<comment>人的ID</comment>
</column>
</many-to-one>
</class>
</hibernate-mapping>
Hibernate 一对一主键双向关联
<?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="entity.Person" table="person">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="24" not-null="true">
<comment>姓名</comment>
</column>
</property>
<!-- cascade="all":在保存person对象的时候,级联保存person对象关联的address对象 -->
<one-to-one name="address" cascade="all" />
</class>
</hibernate-mapping>
<?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="entity.Address" table="address" catalog="mydb">
<id name="id" type="java.lang.Long">
<column name="id" />
<!-- class="foreign": 一对一主键映射中,使用另外一个相关联的对象的标识符 -->
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="detail" type="java.lang.String">
<column name="detail" length="120" not-null="true">
<comment>详细地址</comment>
</column>
</property>
<!-- 表示在address表存在一个外键约束,外键参考相关联的表person -->
<one-to-one name="person" constrained="true" />
</class>
</hibernate-mapping>
Hibernate 一对一连接表双向关联
<hibernate-mapping>
<class name="com.lavasoft.sx._1_1_tab.Person11tab_sx" table="PERSON_11tab_sx">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<join table="join_11tab_sx"
optional="true">
<key column="personid"
unique="true"/>
<many-to-one name="address11tab_sx"
column="addressid"
not-null="true"
unique="true"/>
</join>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.sx._1_1_tab.Address11tab_sx" table="ADDRESS_11tab_sx">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
<join table="join_11tab_sx"
optional="true"
inverse="true">
<key column="addressid"
unique="true"/>
<many-to-one name="person11tab_sx" column="personid"
not-null="true" unique="true"/>
</join>
</class>
</hibernate-mapping>
Hibernate 一对多外键双向关联
hibernate-mapping>
<class name="com.lavasoft.sx._1_n_fk.Person1nfk_sx" table="PERSON_1nfk_sx">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--映射集合属性,关联到持久化类-->
<set name="addresses" inverse="true" cascade="all">
<!--column用于指定外键列名-->
<key column="personid" not-null="true"/>
<!--映射关联类-->
<one-to-many class="com.lavasoft.sx._1_n_fk.Address1nfk_sx"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.sx._1_n_fk.Address1nfk_sx" table="ADDRESS_1nfk_sx">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
<!--映射关联属性,column属性指定外键列名-->
<many-to-one name="person1nfk"
class="com.lavasoft.sx._1_n_fk.Person1nfk_sx"
fetch="select"
cascade="save-update">
<column name="personid" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
Hibernate 一对多连接表双向关联
<hibernate-mapping>
<class name="com.lavasoft.sx._1_n_tab.Person1ntab_sx" table="PERSON_1ntab_sx">
<id name="personid">
<generator class="identity"/>
</id>
<property name="name"/>
<property name="age"/>
<!--映射集合属性,关联到持久化类-->
<!--table="join_1ntab_sx"指定了连接表的名字-->
<set name="addresses"
table="join_1ntab_sx"
cascade="all">
<!--column="personid"指定连接表中关联当前实体类的列名-->
<key column="personid" not-null="true"/>
<!--unique="true"表示当前实体类是"1",不是"n"-->
<many-to-many column="addressid"
unique="true"
class="com.lavasoft.sx._1_n_tab.Address1ntab_sx"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.lavasoft.sx._1_n_tab.Address1ntab_sx"
table="ADDRESS_1ntab_sx">
<id name="addressid">
<generator class="identity"/>
</id>
<property name="addressdetail"/>
<!--映射关联属性,column属性指定外键列名-->
<join table="join_1ntab_sx"
inverse="true"
optional="true">
<key column="addressid"/>
<many-to-one name="person1ntab_sx"
column="personid"
cascade="all"
not-null="true"/>
</join>
</class>
</hibernate-mapping>
Hibernate 多对多双向关联
<hibernate-mapping> <class name="com.lavasoft.sx._n_n.Personnn_sx" table="PERSON_nn_sx"> <id name="personid"> <generator class="identity"/> </id> <property name="name"/> <property name="age"/> <!--映射集合属性,关联到持久化类--> <!--table="join_1ntab_sx"指定了连接表的名字--> <set name="addresses" table="join_nn_sx" cascade="all"> <!--column="personid"指定连接表中关联当前实体类的列名--> <key column="personid" not-null="true"/> <!--column="addressid"是连接表中关联本实体的外键--> <many-to-many column="addressid" class="com.lavasoft.sx._n_n.Addressnn_sx"/> </set> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.lavasoft.sx._n_n.Addressnn_sx" table="ADDRESS_nn_sx"> <id name="addressid"> <generator class="identity"/> </id> <property name="addressdetail"/> <!--table="join_nn_sx"是双向多对多的连接表--> <set name="persons" inverse="true" table="join_nn_sx"> <!--column="addressid"是连接表中关联本实体的外键--> <key column="addressid"/> <many-to-many column="personid" class="com.lavasoft.sx._n_n.Personnn_sx"/> </set> </class> </hibernate-mapping>