Hibernate实体映射文件多对多等关系简单应用技巧
第一步,写注释:
<!--xx属性,本类与Yy(类)的多对一 -->
<!--xx属性,本类与Yy(类)的一对多 -->
<!--xx属性,本类与Yy(类)的多对多 -->
<!--xx属性,本类与Yy(类)的一对一 -->
第二部,拷模版
<!--xx属性,本类与Yy(类)的多对一 -->
<many-to-one name="" class="" column=""></many-to-one>
<!--xx属性,本类与Yy(类)的一对多 -->
<set name="">
<key column=""></key>
<one-to-many class=""/>
</set>
<!--xx属性,本类与Yy(类)的多对多 -->
<set name="" table="">
<key column=""></key>
<many-to-many class="" column=""></many-to-many>
</set>
<!--xx属性,本类与Yy(类)的一对一 -->
<many-to-one name="" class="" column="" unique="true"></many-to-one>
第三步,填空:
<!--xx属性,本类与Yy(类)的多对一 -->
<many-to-one name="xx" class="Yy" column=""></many-to-one>
<!--xx属性,本类与Yy(类)的一对多 -->
<set name="xx">
<key column=""></key>
<one-to-many class="Yy"/>
</set>
<!--xx属性,本类与Yy(类)的多对多 -->
<set name="xx" table="">
<key column=""></key>
<many-to-many class="Yy" column=""></many-to-many>
</set>
<!--xx属性,本类与Yy(类)的一对一 -->
<many-to-one name="xx" class="Yy" column="" unique="true"></many-to-one>
说明:
多对一,一对多中的column的名字自己取,但是对应的两个关系的两个column必须一样;
一般在多的一方,把属性名+Id当成column值,一对多 一方也使用这个column,这样就可以了;
多对多中,tables 是多对多的中间表一般命名中包括两个表名。 key中的column值得是集合外键,指的是引用当前自己表的外键,本对象+Id。 many-to-many指的是引用对方的外键,一般关联对象名+Id;
下面我们给出一个例子:
实体类:
Role
/**
* 实体:岗位
* @author Jelly
*
*/
public class Role implements Serializable{
private Long id;
private String name;
private Set<User> users = new HashSet<User>();
}
hbn.xml
<hibernate-mapping package="com.hqu.oa.domain"> <class name="Role" table="hqu_role"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <!-- users属性,本类与User多对多关系 --> <set name="users" table="hqu_user_role" > <key column="roleId"></key> <many-to-many class="User" column="userId"></many-to-many> </set> </class> </hibernate-mapping>
User:
/**
* 实体:用户
* @author Jelly
*
*/
public class User implements Serializable{
private Long id;
private Department department;// 所属部门
private Set<Role> roles = new HashSet<Role>();
}
User.hbm.xml
<hibernate-mapping package="com.hqu.oa.domain"> <class name="User" table="hqu_user"> <id name="id"> <generator class="native" /> </id> <!-- department属性,本类与Department的多对一关系 --> <many-to-one name="department" class="Department" column="departmentId"></many-to-one> <!-- roles属性,本类与Role的多对多关系 --> <set name="roles" table="hqu_user_role" order-by="roleId" lazy="false"> <key column="userId"></key> <many-to-many class="Role" column="roleId"></many-to-many> </set> </class> </hibernate-mapping>
Department
/**
* 实体:部门
* @author Jelly
*/
public class Department implements Serializable{
private Long id;
private Set<User> users = new HashSet<User>();
private Department parent;
private Set<Department> children = new HashSet<Department>();
}
Department.hbm.xml<hibernate-mapping package="com.hqu.oa.domain"> <class name="Department" table="hqu_department"> <id name="id"> <generator class="native" /> </id> <!-- users属性,本类与User的一对多关系 --> <set name="users"> <key column="departmentId"></key> <one-to-many class="User" /> </set> <!-- parnet属性,本类与Department(上级)的多对一 --> <many-to-one name="parent" class="Department" column="parentId"></many-to-one> <!-- children属性,本类与Department(下级)的一对多关系 --> <set name="children" cascade="delete" order-by="id"> <key column="parentId"></key> <one-to-many class="Department"/> </set> </class> </hibernate-mapping>