我们在使用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对象即可在数据库中创建四张表。