`
konhon
  • 浏览: 1529 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

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

阅读更多
兩個表(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呀. 為什麼不行
分享到:
评论

相关推荐

    关于程序设计的内存分配问题

    不明白内存分配和指针的可以看看,其实这本是我们老师留的一个操作系统科技小论文作业,不知道写什么,干脆把以前收藏的经典C内存分配的文章整理并修改了一下。 此文章有2个用处, 1:这是个小论文,格式完整,大家...

    石头剪子布小程序,谢谢大家

    【描述】:“到底咋回事大家看看哈,谢谢你们了到底咋回事大家看看哈,谢谢你们了到底咋回事大家看看哈,谢谢你们了” 描述中的反复提及可能是在寻求帮助或解释问题,暗示这个小程序可能存在一些不明确或者需要调试...

    VC,MFC做的图书管理系统,有点问题,大家帮忙看一下吧,谢谢了

    描述中提到的“有点问题,大家帮忙看一下吧”意味着这个图书管理系统在运行或编译时遇到了一些问题。通常,这种情况下,开发者会将问题的详细信息或者错误日志记录在一个文本文件中,即“txt文件”,以便其他人能够...

    关于架构设计的一些看法,谢谢

    "关于架构设计的一些看法,谢谢"这个标题暗示了作者将分享个人对架构设计的心得体会,可能涵盖如何简化复杂问题、提升系统效率等方面。描述中提到的“运用一些技巧或设计模式”,揭示了文章可能会深入讨论几种常用的...

    有问题的工程,在这里,谢谢了

    在IT行业中,遇到“有问题的工程”这样的情况是常见的挑战,这通常意味着开发者或团队在进行软件开发过程中遇到了一些未解决的技术难题或者错误。在这个压缩包文件中,包含的可能是一个具体的项目源代码、配置文件...

    重构-改善既有代码的设计

    一直很喜欢重构这本书,但是由于自己记性不太好,书看过之后其中的方法总是记不住,于是想如果有电子版的重构书就好了,工作中遇到重构的问题可以随时打开查阅。在网上搜索了许久,发现重构这本书有英文chm版本的,...

    光盘 琢石成器——Windows环境下32位汇编语言程序设计

    1,不用一上来就说,不是书,妈的,骗我(这个还好了,被骂的多了),我已经说了不是,没注意看吗? 2,连分卷压缩的概念的没,还说怎么解压不了啊?re:有限制大小知道吗? 3,爱下不下,少废话,分数我够了,但是...

    Asp.NET新闻发布系统(毕业设计版)v1.0

    我平时忙于工作,没什么时间来和大家做长时间的交流,今天抽晚上的时间完成这个系统、、、 现在是早晨的三点,我只做了数据库的显示、和增加,还有.NET 2.0下的GridView的分页显示,希望感 兴趣的朋友,能够下载后...

    稀缺机械毕业设计 我要着,没用!分享了

    描述中的“这么好的东西,我要着也没什么用,分享了~谢谢大家的支持啊!”表达的是作者认为这个毕业设计具有很高的价值,但可能由于个人原因不需要,所以选择分享出来供他人学习和参考。这种无私的分享精神在学术界...

    留言程序毕业设计课程设计JSP

    【描述】:“谢谢关照,还不错的,请大家下来看看吧。哈哈……” 这简短的描述虽然没有提供具体的技术细节,但可以看出作者对于项目的满意态度,并且邀请其他人体验和评价。这通常意味着项目已经完成并且功能相对...

    大神是如何总结这一月的?——ppt设计师创意类月度总结模板PPT模板.pptx

    9. 致谢与展望:感谢团队成员、合作伙伴的支持,表达对未来的期待和祝愿,如“谢谢大家再见(●'?'●)身体健康 万事如意”。 在制作PPT时,还要注意以下几点: - 保持简洁:避免过多的文字和复杂的图表,让信息...

    [原作]C语言课程设计-停车场管理系统(详看描述)

    问题描述: 汽车在停车场内按车到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车放在车场的最北端),若车场内已停满车辆,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道...

    网上订餐系统-课程设计项目总结及个人总结(特别声明:是项目总结和我个人的原创总结,目录请忽略,望体谅!)

    网上订餐系统课程设计系列文档之-项目总结和个人总结 ...目录是模板里的请忽略,大家取用关键内容就好,谢谢体谅! 目录是模板文件自带的!单看资源名称和我文档对应的多实在啊!跪求大佬别举报了!。。。TAT。。。哭哭

    Windows程序设计5版chm 大字版 保证看的舒服

    windows 程序设计的大字版本, 由于原来的windows程序设计看的字体很小,所以费力一把,重新编译成新的chm文件,上传给大家享用,谢谢大家

    经典迷人ppt模板,看看便知道

    "经典迷人ppt模板,看看便知道"这一标题暗示了我们正在讨论的是一套具有高度吸引力和广泛适用性的PPT模板,它可能包含了一系列设计精美、布局合理的幻灯片模板,适合多种场合,如商业报告、学术演讲、产品推介等。...

    我的设计软件

    在描述中,“好用的软件打开以后有注册码看看就知道了挺好玩的 谢谢”,这句话表明软件可能不是免费的,需要一个注册码进行激活,同时也暗示用户对软件的易用性和趣味性有一定的认可。 在标签 "youyong" 中,由于这...

    华为模板设计器

    华为模板设计器,大家使用一下,看看好不好用,谢谢

Global site tag (gtag.js) - Google Analytics