论坛首页 入门技术论坛

Hibernate继承映射:每个子类一张表(joined-subclass)表结构

浏览 2007 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-11  

我在前面写了关于继承映射的整个继承树对应一张表的例子,但是他存在以一定的弊端。在数据表中会产生大量的null值。

为了避免这种情况的发生。我们可以这样去做:

<joined-subclass name="Skiller" table="skiller">
   <key column="emp_id"/>
   <property name="skill"></property>
 </joined-subclass>
 
 <joined-subclass name="Sales" table="sales">
   <key column="emp_id"/>
   <property name="sell"></property>
 </joined-subclass>

通过使用joined-subclass映射,对每一个子类生成一张表。

1、domain类:

package com.zhaosoft.domain;

public class Employee {

 private int id;
 private String name;
 private Department 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 Department getDepart() {
  return depart;
 }

 public void setDepart(Department depart) {
  this.depart = depart;
 }

}
2、各个子类:

package com.zhaosoft.domain;

public class Sales extends Employee{

 private int sell;//销售额

 public int getSell() {
  return sell;
 }

 public void setSell(int sell) {
  this.sell = sell;
 }
}

---------------------------------------------

package com.zhaosoft.domain;

public class Skiller extends Employee{
 private String skill;

 public String getSkill() {
  return skill;
 }

 public void setSkill(String skill) {
  this.skill = skill;
 }
}

 

3、映射文件:

<?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.zhaosoft.domain">
     <!-- 当discriminator-value值为0时,为普通员工 -->
 <class name="Employee" discriminator-value="0">  
  <id name="id">
      <!-- 表示主键为自动增长 -->
   <generator class="native"/>
  </id>
 <property name="name" type="string"/>
 <many-to-one name="depart" class="Department" column="depart_id"></many-to-one>
 
 <joined-subclass name="Skiller" table="skiller">
   <key column="emp_id"/>
   <property name="skill"></property>
 </joined-subclass>
 
 <joined-subclass name="Sales" table="sales">
   <key column="emp_id"/>
   <property name="sell"></property>
 </joined-subclass>
 
 </class>
 
</hibernate-mapping>

 

4、测试文件:

public static void add() {
  Session s = null;
  Transaction t=null;
  try {
   s=HibernateUtil.getSession();
   t = s.beginTransaction();
   t.begin();
   Department d = new Department();
   d.setName("销售部");

   Employee employee1 = new Employee();
   employee1.setName("小三");
   employee1.setDepart(d);

   Skiller employee2 = new Skiller();
   employee2.setName("李斯");
   employee2.setSkill("skill");
   employee2.setDepart(d);
   
   Sales employee3 = new Sales();
   employee3.setName("王五");
   employee3.setSell(100);
   employee3.setDepart(d);

   Set<Employee> set=new HashSet<Employee>();
   set.add(employee1);
   set.add(employee2);
   set.add(employee3);
   d.setEmps(set);
   s.save(d);
   s.save(employee1);
   s.save(employee2);
   s.save(employee3);
   
   t.commit();
  } catch (Exception e) {

  } finally {
   if (s != null) {
    s.close();
   }
  }
 }

论坛首页 入门技术版

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