Hibernate中實體類映射文件表與表的關係模版

Hibernate中表與表的關係模板代碼

Hibernate是一個重量級的ORM框架,今天就講講其中配置文件,固然也能夠使用註解配置表示關係。php

一對多的關係

<!-- 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>
複製代碼

須要第三表來維護java

模板實例

實體類

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>
複製代碼

數據表模型

數據表模型

總結:

圖片總結

一、寫註釋

​ 格式爲:?屬性,表達的是本對象與?的?關係。app

​ 例:「department屬性,本對象與Department的多對一」框架

二、模版

多對一

<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>
複製代碼

三、填空

•name屬性:屬性名(註釋中的第1問號)dom

•class屬性:關聯的實體類型(註釋中的第2個問號)this

•column屬性:spa

•<many-to-onecolumn="..">:通常能夠寫成屬性名加Id後綴,如屬性爲department,則column值寫成departmentId。.net

•一對多中的<keycolumn="..">:從關聯的對方(對方是多對一)映射中把column值拷貝過來。hibernate

•多對多中的<keycolumn=「..」>:通常能夠寫成本對象的名加Id後綴,如本對象名爲User,則寫爲userId。code

•多對多中的<many-to-manycolumn=「..」>:通常能夠寫爲關聯對象的名稱加Id後綴。

相關文章
相關標籤/搜索