Hibernate中表與表的關係模板程式碼
Hibernate是一個重量級的ORM框架,今天就講講其中配置檔案,當然也可以使用註解配置表示關係。
一對多的關係
<!-- users屬性 與User的一對多 -->
<set name="users">
<key column="departmentId"></key>
<one-to-many class="User" />
</set>
複製程式碼
多對一關係
<!-- department 使用者與部門的多對一 -->
<many-to-one name="department" class="Department" column="departmentId">
</many-to-one>
複製程式碼
多對多關係
<!-- users屬性,本類與User的多對多 -->
<set name="users" table="itcast_user_role">
<key column="roleId"></key>
<many-to-many class="User" column="userId"></many-to-many>
</set>
複製程式碼
<!-- roles屬性,本類與Role的多對多 -->
<set name="roles" table="itcast_user_role">
<key column="userId"></key>
<many-to-many class="Role" column="roleId"></many-to-many>
</set>
複製程式碼
需要第三表來維護
模板例項
實體類
Department.java
package cn.zzuli.oa.domain;
import java.util.HashSet;
import java.util.Set;
/**
* 部門
* @author LZH
* @date 2017年2月23日
*/
public class Department {
private Long id;
private Set<User> users = new HashSet<User>();
private Department parent;
private Set<Department> children = new HashSet<Department>();
private String name;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Department getParent() {
return parent;
}
public void setParent(Department parent) {
this.parent = parent;
}
public Set<Department> getChildren() {
return children;
}
public void setChildren(Set<Department> children) {
this.children = children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
複製程式碼
Role.java
package cn.zzuli.oa.domain;
import java.util.HashSet;
import java.util.Set;
/**
* 崗位
* @author LZH
* @date 2017年2月23日
*/
public class Role {
private Long id;
private String name;
private String description;
private Set<User> users = new HashSet<User>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
複製程式碼
User.java
package cn.zzuli.oa.domain;
import java.util.HashSet;
import java.util.Set;
/**
* 使用者
* @author LZH
* @date 2017年2月23日
*/
public class User {
private Long id;
private Department department;
private Set<Role> roles = new HashSet<Role>();
private String loginName; // 登入名
private String password; // 密碼
private String name; // 真實姓名
private String gender; // 性別
private String phoneNumber; // 電話號碼
private String email; // 電子郵件
private String description; // 說明
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
複製程式碼
對應配置檔案
Department.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.zzuli.oa.domain">
<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<!-- 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">
<key column="parentId"></key>
<one-to-many class="Department" />
</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="cn.itcast.oa.domain">
<class name="Role" table="itcast_role">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="description"/>
<!-- users屬性,本類與User的多對多 -->
<set name="users" table="user_role">
<key column="roleId"></key>
<many-to-many class="User" column="userId"></many-to-many>
</set>
</class>
</hibernate-mapping>
複製程式碼
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="cn.itcast.oa.domain">
<class name="User" table="itcast_user">
<id name="id">
<generator class="native"/>
</id>
<property name="loginName"/>
<property name="password"/>
<property name="name"/>
<property name="gender" />
<property name="phoneNumber"/>
<property name="email"/>
<property name="description"/>
<!-- department屬性,本類與Department的多對一 -->
<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
<!-- roles屬性,本類與Role的多對多 -->
<set name="roles" table="user_role">
<key column="userId"></key>
<many-to-many class="Role" column="roleId"></many-to-many>
</set>
</class>
</hibernate-mapping>
複製程式碼
資料表模型
總結:
1、寫註釋
格式為:?屬性,表達的是本物件與?的?關係。
例:“department屬性,本物件與Department的多對一”
2、模版
多對一
<many-to-one name="" class="" column=""/>
複製程式碼
一對多
<set name="">
<key column=""></key>
<one-to-many class=""/>
</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字尾。