我们在使用Hibernate框架的时候,非常纠结的地方就是实体和表之间的映射,今天借助汤老师的思路写了小教程,以后配置不用纠结了!
第一步:写注释
格式为:?属性,表达的是本对象与?的?关系。
例:“department属性,本对象与Department的多对一”
第二步:拷模板
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWEAAADSCAIAAAAUtObvAAAVX0lEQVR4nO2dMW/cOBOG1aWzO3dOdZX9C9KmSJD+rr3GUHU4wLC7axL4F6gwkMIIUrlPvcW2AfIDtksT4KqrvJ0rfsUk8405wxFFUVrt+n2KYFemyBFXfDWiwldNAwAAivCLXQcCAFgk0AgAgIehEWHPOYBDAGDnQCMAAB7QCACABzQCAOABjQAAeEAjAAAeFTSibCjOMIChEYtls9n8/vvvm83m/fv3XdednZ3d3Ny8efMmZ8fT09MfP35MHVUIYVBgqQqni9Zszu/VrusuLy+HVltHIyT5ew2NdSh7oRHb7fa3335br9e7DmRWuq67u7v7+PHjer3ebrcXFxf0b++Ok446GVUIYVBgJjNrRG+vtm17d3c3tNqxGhHtmapdV7g0jdhsNq9evXp4eJguHpNnqxF8lVutVnd3d3R+9+44tUbIa++gwEzm1winV7fb7fn5+f39/dnZ2aDzfJRG+AJhFi5LOsrIrH+z2RwfH3PHPT4+vn79+urq6vj4uGka6m6KltM23tI0DSVvdDZcX1/LjSGEtm15Ly0H2+325cuXTdO8ePFivV5T01RD6ofUZVJNy8JHR0f6TE3tqI8up0/M5mRV/nGZcP9E/awr5+Dp16SNPDz0xuqBOT/Njx8/ZNj8OadXzd+oLPjVasXaN+h4uRW9xRtgXED+G9xh2Swvj4jUgaBfjjbSqcCfaYTTRj7VaDxQVfwTykEiP+sfhquldulXlJ8lZplU00EoVNd1ujZzR/Poevskp7mhUKMUHkfFAywV57t37yge7m1zY/XAUj9Nr0b4ver8uEOJJiPylaJQI6Iqog/6T/qvO9eIVI/TL0cnX+ozI0+Ck5MT/l35hJB7tW2rZ4z4hIjyUjNNNcus12uz6ehUM2szd9TN5fRJb3NDyek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ezC08FpOtnJwr2xhafupNx6WWzabu/m5kYvY48Wy/NiUKrzv//+i7aMHxg6sI8fP5pdl6MRs5kARpMR1ZUCGlEZ36vOWYgtC0dnnjaM6/VlSxm66a9DYwtPR0hBbDqAXuMcLW2m2I2kNzAtDY5GzGMCGC2lZyp6Z0Aj6uN73vZ+ps6XeYRp4qB92dgk4ujoaLPZpDSCdvnrr7+48Pfv33M0QoeXH5vEMShLdUKw7gL0lpH3Gn5gQSjveI0w+60gfunrx0eBPCKX3R6C452fOm/4HUWpc0h/Nv3swgR5hBleWWzO5TrVCRKdqVXx7PYD03FOoREFSL8MzEcMZgmHoL3qQuK8kSd627Y83ei7UTaWn11Iz0eEp3OKRI5GmOGVxabt9v7880821DU7QR4OXe3//fffaEvd+Qj6/Pfff5td5/sAzqkR/NRzOmM+aMRM9J43Mse+vb3V02NBnU+OL1vk+pfzXCP/PojDK4stKLu96AqsOyGIPJyFQ28Zj/YB9J9rBMsHcDaNSE1G1AUasccs2ZdtybGBQUAj9pgl+7ItOTYwCGjEXrJkX7YlxwYKgEYAADygEQAAD2gEAMADGgEA8IBGAAA8oBEAAA9ohM2k5l8A7BHQCJtB/wVov0yfABgENOIn0lROr7f10SuO5jenA2AioBGG3QOvNdBuaEEs8qE1S9oYbqIlugDshGetEaYZTPg1GaH90agYLxAmJ5WQuNeAUoDD4PlqhGMqR9JguqGZ3i3OfARuPcC+83w1IiTyCDkZ4fuj8f0F8ghwwDxrjSAipTCNDzjpMOUg2gh1AIcENOIn/FyDn3qmnlbwfIQpHLi5AAcGNOIJkfmX6YbmvOEGrirg8IBGAAA8oBEAAA9oBADAAxoBAPCARgAAPKARAAAPaAQAwMPTCAAAYKARAAAPaAQAwOP/GjHPTQ4AYE+BRgAAPP4HuYRPBJ7eXioAAAAASUVORK5CYII=" alt="" />
第三步:填空:
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
package com.qcf.po; import java.util.HashSet;
import java.util.Set; public class Depart { private int id;
private String name;
//用户
Set<User> users=new HashSet<User>(); //子类部门
Set<Depart> departs=new HashSet<Depart>(); //父类部门
private Depart depart; public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Set<Depart> getDeparts() {
return departs;
}
public void setDeparts(Set<Depart> departs) {
this.departs = departs;
}
public Depart getDepart() {
return depart;
}
public void setDepart(Depart depart) {
this.depart = depart;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Depart(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Depart() {
super();
} }
Role.java
package com.qcf.po; import java.util.HashSet;
import java.util.Set; public class Role { private int id;
private String name;
private String destion; //用户
Set<User> users=new HashSet<User>(); public Set<User> getUser() {
return users;
}
public void setUser(Set<User> users) {
this.users = users;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDestion() {
return destion;
}
public void setDestion(String destion) {
this.destion = destion;
}
public Role(int id, String name, String destion) {
super();
this.id = id;
this.name = name;
this.destion = destion;
} public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Role() {
// TODO Auto-generated constructor stub
}
}
Depart.java
package com.qcf.po; import java.util.HashSet;
import java.util.Set; public class Depart { private int id;
private String name;
//用户
Set<User> users=new HashSet<User>(); //子类部门
Set<Depart> departs=new HashSet<Depart>(); //父类部门
private Depart depart; public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Set<Depart> getDeparts() {
return departs;
}
public void setDeparts(Set<Depart> departs) {
this.departs = departs;
}
public Depart getDepart() {
return depart;
}
public void setDepart(Depart depart) {
this.depart = depart;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Depart(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Depart() {
super();
} }
映射文件:
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.qcf.po">
<class name="User" table="user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" column="username" type="string"></property>
<property name="age" column="userage" type="integer"></property> <!--depart属性,本对象与Depart的多对一 -->
<many-to-one name="depart" class="Depart" column="departId"></many-to-one> <!--roles属性,本对象与Role的多对多 -->
<set name="roles" table="user_role">
<key column="roleId"></key>
<many-to-many class="Role" column="userId"></many-to-many>
</set> </class> </hibernate-mapping>
Role.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.qcf.po">
<class name="Role">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" column="rolename" type="string"></property>
<property name="destion" column="roledestion"></property> <!--users属性,本对象与User的多对多 -->
<set name="users" table="user_role">
<key column="userId"></key>
<many-to-many class="User" column="roleId"/>
</set> </class> </hibernate-mapping>
Depart.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.qcf.po">
<class name="Depart">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" column="departname" type="string"></property> <!--users属性,本对象与User的一对多 -->
<set name="users">
<key column="departId"></key>
<one-to-many class="User"/>
</set> <!--departs属性,本对象与Depart(子类)的一对多 -->
<set name="departs">
<key column="departId"></key>
<one-to-many class="Depart"/>
</set> <!--depart属性,本对象与Depart(父类)的多对一 -->
<many-to-one name="depart" column="departId" class="Depart"></many-to-one> </class> </hibernate-mapping>
直接启动程序,或者获取sessionFactory对象即可在数据库中创建四张表。