论坛首页 入门技术论坛

請大家幫我看看這個設計有沒有什麼問題? 謝謝大家

浏览 1574 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-03-05  
兩個表(Department部門表, Employee員工表)

Department與Employee是雙向的一對多, 多對一的表(通過DepartmentID關聯)
Department還有上下級部門, 形成樹形關系.
另: 在Department中可以設置部門主管(director), 因此部門的director和Employee的Employeeid也形成了多對一的關系
Employee.java如下
package mis.hr.domain;

import java.io.Serializable;

/**
 * @hibernate.class table="Employee" 
 */
public class Employee implements Serializable {

	private static final long serialVersionUID = 1L;

	private String employeeId;
	private String employeeName;
	private String employeeSex;
	private String email;
	private Department department;

	public Employee() {

	}

	/**
	 * @hibernate.id name="employeeId" generator-class="assigned" length="10" 
	 * @return
	 */
	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	/**
	 * @hibernate.property name="employeeName"  length="30"
	 * @return String
	 */
	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	/**
	 * @hibernate.property name="employeeSex"  length="1"
	 * @return String
	 */
	public String getEmployeeSex() {
		return employeeSex;
	}

	public void setEmployeeSex(String employeeSex) {
		this.employeeSex = employeeSex;
	}

	/**
	 * @hibernate.property name="email"  length="100"
	 * @return String
	 */
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @hibernate.many-to-one column="departmentId" class="mis.hr.domain.Department"
	 * @return Department
	 */
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

}

Employee.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>
  <class name="mis.hr.domain.Employee" table="Employee">
    <id name="employeeId" column="employeeId" type="java.lang.String"
      length="10">
      <generator class="assigned">
        <!--  
          To add non XDoclet generator parameters, create a file named 
          hibernate-generator-params-Employee.xml 
          containing the additional parameters and place it in your merge dir. 
        -->
      </generator>
    </id>
    <property name="employeeName" type="java.lang.String" column="employeeName"
      length="30" />
    <property name="employeeSex" type="java.lang.String" column="employeeSex"
      length="1" />
    <property name="email" type="java.lang.String" column="email" length="100" />
    <many-to-one name="department" class="mis.hr.domain.Department"
      cascade="none" outer-join="auto" column="departmentId" />
    <!--
      To add non XDoclet property mappings, create a file named
      hibernate-properties-Employee.xml
      containing the additional properties and place it in your merge dir.
    -->
  </class>
</hibernate-mapping>

Department.java如下
package mis.hr.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * @hibernate.class table="Department" 
 */
public class Department implements Serializable {

	private static final long serialVersionUID = 1L;
	private String departmentId;
	private String departmentName;
	private Employee director;
	private Set employees = new HashSet();
	private Department parentDepartment;
	private Set subDepartments = new HashSet();

	public Department() {

	}

	/**
	 * @hibernate.id name="departmentId" generator-class="assigned" length="10"
	 * @return String
	 */
	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	/**
	 * @hibernate.property name="departmentName" length="30"
	 * @return String
	 */
	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	/**
	 * @hibernate.many-to-one column="director" class="mis.hr.domain.Employee"
	 * @return Employee
	 */
	public Employee getDirector() {
		return director;
	}

	public void setDirector(Employee director) {
		this.director = director;
	}

	/**
	 * @hibernate.set name="employees" table="Employee"
	 * inverse="true" cascade="save-update" lazy="true"
	 * @hibernate.collection-key column="departmentId"
	 * @hibernate.collection-one-to-many class="mis.hr.domain.Employee"
	 * @return Set
	 */
	public Set getEmployees() {
		return employees;
	}

	public void setEmployees(Set employees) {
		this.employees = employees;
	}

	/**
	 * @hibernate.many-to-one column="parentId" class="mis.hr.domain.Department"
	 * @return Department
	 */
	public Department getParentDepartment() {
		return parentDepartment;
	}

	public void setParentDepartment(Department parentDepartment) {
		this.parentDepartment = parentDepartment;
	}

	/**
	 * @hibernate.set name="subDepartments" table="Department"
	 * inverse="true" cascade="save-update" lazy="true"
	 * @hibernate.collection-key column="parentId"
	 * @hibernate.collection-one-to-many class="mis.hr.domain.Department"
	 *
	 * @return Set 
	 */
	public Set getSubDepartments() {
		return subDepartments;
	}

	public void setSubDepartments(Set subDepartments) {
		this.subDepartments = subDepartments;
	}

}

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>
  <class name="mis.hr.domain.Department" table="Department">
    <id name="departmentId" column="departmentId" type="java.lang.String"
      length="10">
      <generator class="assigned">
        <!--  
          To add non XDoclet generator parameters, create a file named 
          hibernate-generator-params-Department.xml 
          containing the additional parameters and place it in your merge dir. 
        -->
      </generator>
    </id>
    <property name="departmentName" type="java.lang.String"
      column="departmentName" length="30" />
    <many-to-one name="director" class="mis.hr.domain.Employee" cascade="none"
      outer-join="auto" column="director" />
    <set name="employees" table="Employee" lazy="true" inverse="true"
      cascade="save-update" sort="unsorted">
      <key column="departmentId"></key>
      <one-to-many class="mis.hr.domain.Employee" />
    </set>
    <many-to-one name="parentDepartment" class="mis.hr.domain.Department"
      cascade="none" outer-join="auto" column="parentId" />
    <set name="subDepartments" table="Department" lazy="true" inverse="true"
      cascade="save-update" sort="unsorted">
      <key column="parentId"></key>
      <one-to-many class="mis.hr.domain.Department" />
    </set>
    <!--
      To add non XDoclet property mappings, create a file named
      hibernate-properties-Department.xml
      containing the additional properties and place it in your merge dir.
    -->
  </class>
</hibernate-mapping>


測試的代碼
		Department department = new Department();
		department.setDepartmentId("01");
		department.setDepartmentName("生產部");

		Department department1 = new Department();
		department1.setDepartmentId("0101");
		department1.setDepartmentName("PMC");

		Employee employee = new Employee();
		employee.setEmployeeId("05-0059");
		employee.setEmployeeName("XXXXX");
		employee.setEmployeeSex("0");
		employee.setEmail("konhon@gmail.com");
		employee.setDepartment(department);
		department.getEmployees().add(employee);
		department.setDirector(employee);

		Employee employee1 = new Employee();
		employee1.setEmployeeId("05-0055");
		employee1.setEmployeeName("XXXXX");
		employee1.setEmployeeSex("0");
		employee1.setDepartment(department1);
		department1.getEmployees().add(employee1);
		department1.setDirector(employee1);

		department1.setParentDepartment(department);
		department.getSubDepartments().add(department1);

		departmentDao.saveDepartment(department);
		departmentDao.saveDepartment(department1);


hibernate產生的SQL語句
[@APPNAME@] DEBUG [main] SQL.log(393) | select department_.departmentId, department_.departmentName as departme2_0_, department_.director as director0_, department_.parentId as parentId0_ from Department department_ where department_.departmentId=?
[@APPNAME@] DEBUG [main] SQL.log(393) | select employee_.employeeId, employee_.employeeName as employee2_1_, employee_.employeeSex as employee3_1_, employee_.email as email1_, employee_.departmentId as departme5_1_ from Employee employee_ where employee_.employeeId=?
[@APPNAME@] DEBUG [main] SQL.log(393) | select department_.departmentId, department_.departmentName as departme2_0_, department_.director as director0_, department_.parentId as parentId0_ from Department department_ where department_.departmentId=?
[@APPNAME@] DEBUG [main] SQL.log(393) | select employee_.employeeId, employee_.employeeName as employee2_1_, employee_.employeeSex as employee3_1_, employee_.email as email1_, employee_.departmentId as departme5_1_ from Employee employee_ where employee_.employeeId=?
[@APPNAME@] DEBUG [main] SQL.log(393) | insert into Department (departmentName, director, parentId, departmentId) values (?, ?, ?, ?)
[@APPNAME@] DEBUG [main] SQL.log(393) | insert into Employee (employeeName, employeeSex, email, departmentId, employeeId) values (?, ?, ?, ?, ?)
[@APPNAME@] DEBUG [main] SQL.log(393) | insert into Department (departmentName, director, parentId, departmentId) values (?, ?, ?, ?)
[@APPNAME@] DEBUG [main] SQL.log(393) | insert into Employee (employeeName, employeeSex, email, departmentId, employeeId) values (?, ?, ?, ?, ?)
[@APPNAME@] DEBUG [main] SQL.log(393) | update Department set departmentName=?, director=?, parentId=? where departmentId=?
[@APPNAME@] DEBUG [main] SQL.log(393) | update Department set departmentName=?, director=?, parentId=? where departmentId=?



我想問:
1. 為什麼insert前還要執行Select查詢, 是不是我的generator-class="assigned"的緣故?
2. 後面的update語句有沒有辦法優化成全部用insert語句完成, 我已經指定了inverse呀. 為什麼不行
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics