Hibernate实体映射
1.写注释
多对一 |
<many-to-one name=“” class=“” column=“”/> |
一对多(Set) |
<set name=""> <key column=""></key> <one-to-many class=""/> </set> |
多对多(Set) |
<set name="" table=""> <key column=""></key> <many-to-many class="" column=""/> </set> |
示例:
实体类:
public class User {
private Long id;
private Department department;
private Set<Role> roles = new HashSet<Role>();
.......
}
public class Department {
private Long id;
private Set<User> users = new HashSet<User>();
private Department parent;
private Set<Department> children = new HashSet<Department>();
.......
}
public class Role {
private Long id;
private Set<User> users = new HashSet<User>();
private Set<Privilege> privileges = new HashSet<Privilege>();
.........
}
public class Privilege {
private Long id;
private Set<Role> roles = new HashSet<Role>();
private Privilege parent;
private Set<Privilege> children = new HashSet<Privilege>();
.........
}
映射文件:
<hibernate-mapping package="cn.usst.oa.domain">
<class name="User" table="usst_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="usst_user_role">
<key column="userId"></key>
<many-to-many class="Role" column="roleId"></many-to-many>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="cn.usst.oa.domain">
<class name="Department" table="usst_department">
<id name="id">
<generator class="native"/>
</id>
<!-- users属性,表示我与User的一对多关系 -->
<set name="users">
<key column="departmentId"></key>
<one-to-many class="User"/>
</set>
<!-- parent属性,表示我与Department(上级)的多对一关系 -->
<many-to-one name="parent" class="Department" column="parentId"></many-to-one>
<!-- children属性,表示我与Department(下级)的一对多关系 -->
<set name="children" cascade="delete">
<key column="parentId"></key>
<one-to-many class="Department"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="cn.usst.oa.domain">
<class name="Role" table="usst_role">
<id name="id">
<generator class="native" />
</id>
<!-- users属性,表达了我与User的多对多关系 -->
<set name="users" table="usst_user_role">
<key column="roleId"></key>
<many-to-many class="User" column="userId"></many-to-many>
</set>
<!-- privileges属性,表达了我与Privilege的多对多关系 -->
<set name="privileges" table="itcast_role_privilege">
<key column="roleId"></key>
<many-to-many class="Privilege" column="privilegeId"></many-to-many>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="cn.usst.oa.domain">
<class name="Privilege" table="usst_privilege">
<id name="id">
<generator class="native" />
</id>
<!-- roles属性,表达了我与Role的多对多关系 -->
<set name="roles" table="usst_role_privilege">
<key column="privilegeId"></key>
<many-to-many class="Role" column="roleId"></many-to-many>
</set>
<!-- parent属性,表达了我与Privilege(上级)的多对一关系 -->
<many-to-one name="parent" class="Privilege" column="parentId"></many-to-one>
<!-- children属性,表达了我与Privilege(下级)的一对多关系 -->
<set name="children" order-by="id ASC">
<key column="parentId"></key>
<one-to-many class="Privilege" />
</set>
</class>
</hibernate-mapping>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。