1 一对一映射
1.1 单向映射
- 主键映射
实体类:User/IdCard
class User{
private Integer userId;
private String name;
private IdCard idCard;
....setter/getter..
}
class IdCard{
private Integer cardId;
private String number;
....setter/getter..
}
hibernate配置
- idCard.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="IdCard" >
<id name="cardId" type="integer">
<generator class="native" />
</id>
<property name="name">
</class>
</hibernate-mapping>
- user.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="User" >
<id name="userId" type="integer">
<!-- 采用foreign策略,foreign会自动关联对象的主键 --!>
<generator class="foreign" >
<!-- property之关联对象 --!>
<param name="property" >idCard</param>
</generator>
</id>
<property name="name">
<!--
one-to-one指示hibernate如何加载其关联对象,根据对端的主键来加载关联对象
constrained=“true”表示当前主键(user的主键)也是一个外键,参考了对端的主键(idCard的
主键),即生成相应的外键约束语句。
--!>
<one-to-one name="idCard" constrained="true" />
</class>
</hibernate-mapping>
- 外键映射
实体类:User/IdCard
和主键映射实体类相同
hibernate配置
idCard.hbm.xml:和主键映射相同
user.hbm.xml:
<hibernate-mapping package="com.user">
<class name="User" >
<id name="userId" type="integer">
<column name="user_id" />
<generator class="native" >
</generator>
</id>
<property name="name">
<many-to-one name='idCard' unique=’true‘ />
</class>
</hibernate-mapping>
1.2 双向映射
- 主键映射
实体类:User/IdCard
class User{
private Integer userId;
private String name;
private IdCard idCard;
....setter/getter..
}
class IdCard{
private Integer cardId;
private String number;
private User user;
....setter/getter..
}
hibernate配置
- idCard.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="IdCard" >
<id name="cardId" type="integer">
<generator class="native" />
</id>
<property name="name">
<one-to-one name="user" />
</class>
</hibernate-mapping>
- user.hbm.xml:
<hibernate-mapping package="com.user">
<class name="User" >
<id name="userId" type="integer">
<column name="user_id" />
<!-- 采用foreign策略,foreign会自动关联对象的主键 --!>
<generator class="foreign" >
<!-- property之关联对象 --!>
<param name="property" >idCard</param>
</generator>
</id>
<property name="name">
<!--
one-to-one指示hibernate如何加载其关联对象,根据对端的主键来加载关联对象
constrained=“true”表示当前主键(user的主键)也是一个外键,参考了对端的主键(idCard的
主键),即生成相应的外键约束语句。
--!>
<one-to-one name="idCard" constrained="true" />
</class>
</hibernate-mapping>
- 外键映射
实体类:User/IdCard
和主键映射实体类相同
hibernate配置
- idCard.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="IdCard" >
<id name="cardId" type="integer">
<generator class="native" />
</id>
<property name="name">
<!-- property-ref属性为关系字段的名称(user中的idCard属性) --!>
<one-to-one name="user" property-ref="idCard" />
</class>
</hibernate-mapping>
- user.hbm.xml:
<hibernate-mapping package="com.user">
<class name="User" >
<id name="userId" type="integer">
<column name="user_id" />
<generator class="native" >
</generator>
</id>
<property name="name">
<many-to-one name='idCard' unique=’true‘ />
</class>
</hibernate-mapping>
2 一对多映射
实体类:User/Classes
class User{
private Integer userId;
private String name;
....setter/getter..
}
class Classes{
private Integer classId;
private String name;
private Set<User> users;
....setter/getter..
}
hibernate配置
- classes.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="Classes" >
<id name="classesId" type="integer">
<generator class="native" />
</id>
<property name="name">
<set name="users">
<key column="classesId"/>
<one-to-many class="User">
</set>
</class>
</hibernate-mapping>
3 多对一映射
实体类:User/Classes
class User{
private Integer userId;
private String name;
private Classes classes;
....setter/getter..
}
class Classes{
private Integer classId;
private String name;
....setter/getter..
}
hibernate配置
- user.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="User" >
<id name="userId" type="integer">
<generator class="native" />
</id>
<property name="name">
<many-to-one name="classes" />
</class>
</hibernate-mapping>
4 双向一对多映射
实体类:User/Classes
class User{
private Integer userId;
private String name;
private Classes classes;
....setter/getter..
}
class Classes{
private Integer classId;
private String name;
private Set<User> users.
....setter/getter..
}
hibernate配置
- user.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="User" >
<id name="userId" type="integer">
<generator class="native" />
</id>
<property name="name">
<many-to-one name="classes" column="classesId"/>
</class>
</hibernate-mapping>
- classes.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="Classes" >
<id name="classesId" type="integer">
<generator class="native" />
</id>
<property name="name">
<set name="users">
<key column="classesId"/>
<one-to-many class="User">
</set>
</class>
</hibernate-mapping>
5 多对多映射
5.1 单向映射
实体类:User/Classes
class User{
private Integer userId;
private String name;
private Set<Role> roles;
....setter/getter..
}
class Role{
private Integer classId;
private String name;
private Set<User> users.
....setter/getter..
}
hibernate配置
- user.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="User" >
<id name="userId" type="integer">
<generator class="native" />
</id>
<property name="name">
<!-- 多对多映射产生一张中间表 --!>
<set name="roles" table="t_user_role">
<!-- 对应的user表的外键--!>
<key name="user_id" />
<!-- 对应的role表的外键--!>
<many-to-many class="Role" column="role_id"/>
</set>
</class>
</hibernate-mapping>
5.2 双向映射
实体类:User/Classes
class User{
private Integer userId;
private String name;
private Set<Role> roles;
....setter/getter..
}
class Role{
private Integer classId;
private String name;
private Set<User> users.
....setter/getter..
}
hibernate配置
- user.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="User" >
<id name="userId" type="integer">
<generator class="native" />
</id>
<property name="name">
<set name="roles" table="t_user_role">
<key name="user_id" />
<many-to-many class="Role" column="role_id"/>
</set>
</class>
</hibernate-mapping>
- role.hbm.xml:
<hibernate-mapping package="com.entity">
<class name="Role" >
<id name="roleId" type="integer">
<generator class="native" />
</id>
<property name="name">
<set name="users" table="t_user_role">
<key name="role_id" />
<many-to-many class="User" column="user_id"/>
</set>
</class>
</hibernate-mapping>