Hibernate实体映射配置(XML)简单三步完美配置

我们在使用Hibernate框架的时候,非常纠结的地方就是实体和表之间的映射,今天借助汤老师的思路写了小教程,以后配置不用纠结了!

第一步:写注释

  格式为:?属性,表达的是本对象与?的?关系。
  例:“department属性,本对象与Department的多对一”

第二步:拷模板

  

第三步:填空:

  name属性:属性名(注释中的第1问号)
  class属性:关联的实体类型(注释中的第2个问号)
  column属性:
    <many-to-one column="..">:一般可以写成属性名加Id后缀,如属性为department,则column值写成departmentId。
    一对多中的<key column="..">:从关联的对方(对方是多对一)映射中把column值拷贝过来。
    多对多中的<key column=“..”>:一般可以写成本对象的名加Id后缀,如本对象名为User,则写为userId。
    多对多中的<many-to-many column=“..”>:一般可以写为关联对象的名称加Id后缀。

应用实例:

  用户、角色、部门之间的关系映射

  用户: ID 、名称

  角色:ID、角色名、描述

  部门:ID、部门名

  关系:

    用户和角色是多对多关系,用户和部门是多对一关系,部门自关联是多对一/一对多关系

  测试代码:

    实体类:(写实体类时候一定不要忘记给每个属性添加setter、getter方法,否则创建不了sessionFactory等对象

     User.java

 1 package com.qcf.po;
 2
 3 import java.util.HashSet;
 4 import java.util.Set;
 5
 6 public class Depart {
 7
 8     private int id;
 9     private String name;
10     //用户
11     Set<User> users=new HashSet<User>();
12
13     //子类部门
14     Set<Depart> departs=new HashSet<Depart>();
15
16     //父类部门
17     private Depart depart;
18
19
20     public Set<User> getUsers() {
21         return users;
22     }
23     public void setUsers(Set<User> users) {
24         this.users = users;
25     }
26     public Set<Depart> getDeparts() {
27         return departs;
28     }
29     public void setDeparts(Set<Depart> departs) {
30         this.departs = departs;
31     }
32     public Depart getDepart() {
33         return depart;
34     }
35     public void setDepart(Depart depart) {
36         this.depart = depart;
37     }
38     public int getId() {
39         return id;
40     }
41     public void setId(int id) {
42         this.id = id;
43     }
44     public String getName() {
45         return name;
46     }
47     public void setName(String name) {
48         this.name = name;
49     }
50     public Depart(int id, String name) {
51         super();
52         this.id = id;
53         this.name = name;
54     }
55     public Depart() {
56         super();
57     }
58
59
60 }

     Role.java

 1 package com.qcf.po;
 2
 3 import java.util.HashSet;
 4 import java.util.Set;
 5
 6 public class Role {
 7
 8     private int id;
 9     private String name;
10     private String destion;
11
12     //用户
13     Set<User> users=new HashSet<User>();
14
15
16     public Set<User> getUser() {
17         return users;
18     }
19     public void setUser(Set<User> users) {
20         this.users = users;
21     }
22     public int getId() {
23         return id;
24     }
25     public void setId(int id) {
26         this.id = id;
27     }
28     public String getName() {
29         return name;
30     }
31     public void setName(String name) {
32         this.name = name;
33     }
34     public String getDestion() {
35         return destion;
36     }
37     public void setDestion(String destion) {
38         this.destion = destion;
39     }
40     public Role(int id, String name, String destion) {
41         super();
42         this.id = id;
43         this.name = name;
44         this.destion = destion;
45     }
46
47     public Set<User> getUsers() {
48         return users;
49     }
50     public void setUsers(Set<User> users) {
51         this.users = users;
52     }
53     public Role() {
54         // TODO Auto-generated constructor stub
55     }
56 }

     Depart.java

 1 package com.qcf.po;
 2
 3 import java.util.HashSet;
 4 import java.util.Set;
 5
 6 public class Depart {
 7
 8     private int id;
 9     private String name;
10     //用户
11     Set<User> users=new HashSet<User>();
12
13     //子类部门
14     Set<Depart> departs=new HashSet<Depart>();
15
16     //父类部门
17     private Depart depart;
18
19
20     public Set<User> getUsers() {
21         return users;
22     }
23     public void setUsers(Set<User> users) {
24         this.users = users;
25     }
26     public Set<Depart> getDeparts() {
27         return departs;
28     }
29     public void setDeparts(Set<Depart> departs) {
30         this.departs = departs;
31     }
32     public Depart getDepart() {
33         return depart;
34     }
35     public void setDepart(Depart depart) {
36         this.depart = depart;
37     }
38     public int getId() {
39         return id;
40     }
41     public void setId(int id) {
42         this.id = id;
43     }
44     public String getName() {
45         return name;
46     }
47     public void setName(String name) {
48         this.name = name;
49     }
50     public Depart(int id, String name) {
51         super();
52         this.id = id;
53         this.name = name;
54     }
55     public Depart() {
56         super();
57     }
58
59
60 }

    映射文件:

     User.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping
 6     package="com.qcf.po">
 7     <class name="User" table="user">
 8         <id name="id">
 9             <generator class="native"></generator>
10         </id>
11         <property name="name" column="username" type="string"></property>
12         <property name="age"  column="userage" type="integer"></property>
13
14         <!--depart属性,本对象与Depart的多对一  -->
15         <many-to-one name="depart" class="Depart" column="departId"></many-to-one>
16
17         <!--roles属性,本对象与Role的多对多  -->
18         <set name="roles" table="user_role">
19             <key column="roleId"></key>
20             <many-to-many class="Role" column="userId"></many-to-many>
21         </set>
22
23     </class>
24
25
26 </hibernate-mapping>

     Role.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping
 6     package="com.qcf.po">
 7     <class name="Role">
 8         <id name="id">
 9             <generator class="native"></generator>
10         </id>
11         <property name="name" column="rolename" type="string"></property>
12         <property name="destion" column="roledestion"></property>
13
14         <!--users属性,本对象与User的多对多  -->
15         <set name="users" table="user_role">
16             <key column="userId"></key>
17             <many-to-many class="User" column="roleId"/>
18         </set>
19
20     </class>
21
22
23 </hibernate-mapping>

    Depart.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping
 6     package="com.qcf.po">
 7     <class name="Depart">
 8         <id name="id">
 9             <generator class="native"></generator>
10         </id>
11         <property name="name" column="departname" type="string"></property>
12
13         <!--users属性,本对象与User的一对多  -->
14         <set name="users">
15             <key column="departId"></key>
16             <one-to-many class="User"/>
17         </set>
18
19         <!--departs属性,本对象与Depart(子类)的一对多  -->
20         <set name="departs">
21             <key column="departId"></key>
22             <one-to-many class="Depart"/>
23         </set>
24
25         <!--depart属性,本对象与Depart(父类)的多对一  -->
26         <many-to-one name="depart" column="departId" class="Depart"></many-to-one>
27
28     </class>
29
30
31 </hibernate-mapping>

   直接启动程序,或者获取sessionFactory对象即可在数据库中创建四张表。

  

时间: 2024-08-10 05:46:39

Hibernate实体映射配置(XML)简单三步完美配置的相关文章

Hibernate实体映射文件多对多等关系简单应用技巧

第一步,写注释: <!--xx属性,本类与Yy(类)的多对一 --> <!--xx属性,本类与Yy(类)的一对多 --> <!--xx属性,本类与Yy(类)的多对多 --> <!--xx属性,本类与Yy(类)的一对一 --> 第二部,拷模版 <!--xx属性,本类与Yy(类)的多对一 --> <many-to-one name="" class="" column="">&l

Hexo + Serverless Framework,简单三步搭建你的个人博客

很多人都想拥有自己的个人博客,还得看起来漂亮.酷酷的.尤其对开发者来说,不仅可以分享技术(装)心得(逼),面试的时候还能成为加分.这里介绍两款好用的神器,不用忙前(前端)忙后(后端),简单3min即可搞定,本文免费分享给大家. PS:不会写代码?没有备案的域名?没有服务器?在这里,这些都不是事儿! 工具介绍 Serverless Framework:Serverless Framework 是业界非常受欢迎的无服务器应用框架,开发者无需关心底层资源即可部署完整可用的 Serverless 应用架

Hibernate实体映射技巧总结

初学者有没有感觉在写Hibernate实习映射的时候,被各种的many-to-one set one-to-many搞乱了头脑呢? 下面只需要三部,轻轻松松搞定. 1,写注释 格式为:(1?)属性,是本类与(2?)的(3?)关系. <span style="font-size:14px;">例如: <!-- users属性,本类与User的一对多 --> <!-- parent属性,本类与Department的多对一 --> <!-- role

Hibernate实体映射

Hibernate实体映射步骤 1.写注释 ?格式为:?属性,表达的是本对象与?的?关系. 2,拷模板: 多对一 <many-to-one name="" class="" column=""/> 一对多(Set) <set name=""> <key column=""></key> <one-to-many class=""/&g

Hibernate实体映射模板

1,写注释 ?格式为:?属性,表达的是本对象与?的?关系. ?例:"department属性,本对象与Department的多对一" 2,拷模板: 多对一 <many-to-one name="" class="" column=""/> 一对多 (Set) <set name=""> <key column=""></key> <o

流程总结:Hibernate实体映射

本文内容来自:<传智播客-OA项目> 1,写注释格式为:?属性,表达的是本对象与?的?关系.例:“department属性,本对象与Department的多对一”2,拷模板: 3,填空:name属性:属性名(注释中的第1问号)class属性:关联的实体类型(注释中的第2个问号)column属性:<many-to-one column="..">:一般可以写成属性名加Id后缀,如属性为department,则column值写成departmentId.一对多中的&

简单三步学会如何将excel转换成word

对于Excel和Word之间的转换问题,我们需要下载一款叫迅捷的PDF转换器,它可以帮我们解决这个问题.对于专业的事情就用该交给专业的软件来做,社会分工越来越精细化,时间就是金钱,语气自己浪费时间做笨功夫,不如用这个时间去做你擅长的有价值的事情,然后把这件事交给别人. 操作非常简单,只用三步就完成了:     1.首先下载安装迅捷PDF转换软件,双击打开软件,在软件左侧选择要转换的类型"文件转Word",然后点击上面的"添加文件"按钮,在弹出的界面上选择要转换的Ex

报表页面集成天气,简单三步,一看就懂

条条框框的报表页面枯燥乏味?不妨给页面加点“新意”! 前阵子,在看天气预报的时候,发现免费天气预报的调用代码,瞬间想到可以给我开发的报表“润润色”. 一共三个步骤,教你手到擒来 第一步 先去天气资源提供者(自行百度)挑选适合自己页面的预报 第二步 copy框中iframe标签. JS代码: document.writeln("<iframe name=\"weather_inc\" src=\"http:\/\/i.tianqi.com\/index.php?

简单三步,从零开始做自媒体,新手快速上手

对于很多人来说想要做自媒体,从过这个平台为自己获取一份额外的收入,但是在这无数的自媒体人中有的人收入可观,而有的人做了一段时间就给放弃了,觉得没有流量收益又浪费时间.那么对于我们很多刚接触还是已经在做的如何才能做自媒体呢?简单来说可以分为三个步骤 选择领域在进行好最基本的媒体账户注册之后,我们首先就是需要选择要做的领域,领域的选择是非常重要的,首先我们要明确自己喜欢哪方面或者擅长什么,不要说写着写着就不知道做什么的.然后就是要考虑所选择领域的市场需求度,如果说没有多少人需要或者关注这个方面的比较