标签:
Many to One:
User.hbm.xml:
<hibernate-mapping> <class name="com.bjpowernode.hibernate.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <many-to-one name="group" column="groupid" cascade="save-update"/> </class> </hibernate-mapping>
Group.hbm.xml:
<hibernate-mapping> <class name="com.bjpowernode.hibernate.Group" table="t_group"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping>
One to One:
- 方式一:主键关联——让两个对象具有相同的主键值,以表明它们之间的一对一对应的关系;数据库表不会有额外的字段来维护它们之间的关系,仅通过表的主键来关联
【单向】
- 图:
代码:
IdCard.hbm.xml:
<hibernate-mapping>
<class name="com.bjpowernode.hibernate.IdCard" table="t_idCard">
<id name="id">
<generator class="native"/>
</id>
<property name="cardNo"/>
</class>
</hibernate-mapping>
Person.hbm.xml:
<hibernate-mapping><class name="com.bjpowernode.hibernate.Person" table="t_person"><id name="id"><!-- 采用foreign生成策略,forgeign会取得关联对象的标识 --><generator class="foreign"><!-- property只关联对象 --><param name="property">idCard</param></generator></id><property name="name"/><!--one-to-one指示hibernate如何加载其关联对象,默认根据主键加载也就是拿到关系字段值,根据对端的主键来加载关联对象constrained="true表示,当前主键(person的主键)还是一个外键参照了对端的主键(IdCard的主键),也就是会生成外键约束语句--><one-to-one name="idCard" constrained="true"/></class></hibernate-mapping>
【双向】
- 图:
- 核心代码:
Person.hbm.xml:
<hibernate-mapping><class name="com.bjpowernode.hibernate.Person" table="t_person"><id name="id"><!-- 采用foreign生成策略,forgeign会取得关联对象的标识 --><generator class="foreign"><!-- property只关联对象 --><param name="property">idCard</param></generator></id><property name="name"/><!--one-to-one指示hibernate如何加载其关联对象,默认根据主键加载也就是拿到关系字段值,根据对端的主键来加载关联对象constrained="true表示,当前主键(person的主键)还是一个外键参照了对端的主键(IdCard的主键),也就是会生成外键约束语句--><one-to-one name="idCard" constrained="true"/></class></hibernate-mapping>
IdCard.hbm.xml:
<hibernate-mapping><class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"><id name="id"><generator class="native"/></id><property name="cardNo"/><one-to-one name="person"/></class></hibernate-mapping>
- 方式二:唯一外键关联——外键关联,本来是用于多对一的配置,但是如果加上唯一的限制之后,也可以用来表示一对一关联关系。
【单向】
- 图:
- 核心代码代码:
IdCard.hbm.xml:
<hibernate-mapping><class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"><id name="id"><generator class="native"/></id><property name="cardNo"/></class></hibernate-mapping>
Person.hbm.xml:
<hibernate-mapping><class name="com.bjpowernode.hibernate.Person" table="t_person"><id name="id"><generator class="native"/></id><property name="name"/><many-to-one name="idCard" unique="true"/></class></hibernate-mapping>
【双向】
- 图:
- 核心代码:
IdCard.hbm.xml:
<hibernate-mapping><class name="com.bjpowernode.hibernate.IdCard" table="t_idCard"><id name="id"><generator class="native"/></id><property name="cardNo"/><one-to-one name="person" property-ref="idCard"/></class></hibernate-mapping>
Person.hbm.xml:
<hibernate-mapping><class name="com.bjpowernode.hibernate.Person" table="t_person"><id name="id"><generator class="native"/></id><property name="name"/><many-to-one name="idCard" unique="true"/></class></hibernate-mapping>
Many to Many:
【单向】
- 图:
Role.hbm.xml:
<hibernate-mapping>
<class name="com.bjpowernode.hibernate.Role" table="t_role">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
User.hbm.xml:
<hibernate-mapping>
<class name="com.bjpowernode.hibernate.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="roles" table="t_user_role">
<key column="user_id"/>
<many-to-many class="com.bjpowernode.hibernate.Role" column="role_id" />
</set>
</class>
</hibernate-mapping>
【双向】
User.hbm.xml:
<hibernate-mapping>
<class name="com.bjpowernode.hibernate.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="roles" table="t_user_role">
<key column="user_id" not-null="true"/>
<many-to-many class="com.bjpowernode.hibernate.Role" column="role_id" />
</set>
</class>
</hibernate-mapping>
Role.hbm.xml:
<hibernate-mapping>
<class name="com.bjpowernode.hibernate.Role" table="t_role">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="users" table="t_user_role">
<key column="role_id" not-null="true"/>
<many-to-many class="com.bjpowernode.hibernate.User" column="user_id"/>
</set>
</class>
</hibernate-mapping>
标签:
原文地址:http://blog.csdn.net/wang13667539325/article/details/44225311