hibernate关联映射处理的设计问题
1:关联方向 Directionality
2:阶数 Multiplicity
3:集合 Collection
A:一对一主键单向关联(Person/IdCard)配置文件
<hibernate-mapping package="com.xyh.hibernate">
<class name="Person" table="cyy_person">
<id name="id">
<generator class="foreign">
<param name="property">card</param>
</generator>
</id>
<property name="username"></property>
<!--
one-to-one 指示hibernate怎么加载它的关联对象,默认的是根据主键关联加载
constrained="true"表明当前主键存在一个约束,person的主键作为外键参照了idCard的主键
默认了Casade=“True”属性
-->
<one-to-one name="card" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.xyh.hibernate">
<class name="IdCard" table="cyy_idcard">
<id name="id">
<generator class="native"/>
</id>
<property name="cardNo"></property>
</class>
</hibernate-mapping>
B:一对一主键双向关联(Person/IdCard)配置文件
<hibernate-mapping package="com.xyh.hibernate">
<class name="Person" table="cyy_person">
<id name="id">
<generator class="foreign">
<param name="property">card</param>
</generator>
</id>
<property name="username"></property>
<!--
one-to-one 指示hibernate怎么加载它的关联对象,默认的是根据主键关联加载
constrained="true"表明当前主键存在一个约束,person的主键作为外键参照了idCard的主键
默认了Casade=“True”属性
-->
<one-to-one name="card" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.xyh.hibernate">
<class name="IdCard" table="cyy_idcard">
<id name="id">
<generator class="native"/>
</id>
<property name="cardNo"></property>
<one-to-one name="person"></one-to-one>
</class>
</hibernate-mapping>
C:一对一外键单向关联(Person/IdCard)配置文件
<hibernate-mapping package="com.xyh.hibernate">
<class name="Person" table="cyy_person">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<many-to-one name="card" unique="true" ></many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.xyh.hibernate">
<class name="IdCard" table="cyy_idcard">
<id name="id">
<generator class="native"></generator>
</id>
<property name="cardNo"></property>
</class>
</hibernate-mapping>
D:一对一外键双向关联(Person/IdCard)配置文件
<hibernate-mapping package="com.xyh.hibernate">
<class name="Person" table="cyy_person">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<!--unique="true"指定唯一参考属性-->
<many-to-one name="card" unique="true"></many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.xyh.hibernate">
<class name="IdCard" table="cyy_idcard">
<id name="id">
<generator class="native"></generator>
</id>
<property name="cardNo"></property>
<!--property-ref 指定外键参考属性-->
<one-to-one name="person" property-ref="card"></one-to-one>
</class>
</hibernate-mapping>
E:多对一关联(Company/Employee)配置文件
<hibernate-mapping package="com.xyh.hibernate">
<class name="Company" table="cyy_company">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.xyh.hibernate">
<class name="Employee" table="cyy_employee">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<many-to-one name="company" column="companyId"></many-to-one>
</class>
</hibernate-mapping>
只要处理好对象之间的关系就OK了.