Hibernate实体映射步骤
1 .写注释
1 .写注释
•
格式为:?属性,表达的是本对象与?的?关系。
2
,拷模板:
多对一 | <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> |
3
,填空:
•
name
属性:属性名(注释中的第
1
问号)
•
class
属性:关联的实体类型(注释中的第
2
个问号)
•
column
属性:
•
<many-to-onecolumn="..">
:一般可以写成属性名加
Id
后缀,如属性为
department
,则
column
值写成
departmentId
。
•
一对多中的
<keycolumn="..">
:从关联的对方(对方是多对一)映射中把
column
值拷贝过来。
•
多对多中的
<keycolumn=“..”>
:一般可以写成本对象的名加
Id
后缀,如本对象名为
User
,则写为
userId
。
•
多对多中的
<many-to-manycolumn=“..”>
:一般可以写为关联对象的名称加
Id
后缀。
完成
示例:
实体类:
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>
示例:
实体类:
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>