`
zhaosoft
  • 浏览: 183657 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多

我在前面写了关于继承映射的整个继承树对应一张表的例子,但是他存在以一定的弊端。在数据表中会产生大量的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();
   }
  }
 }

分享到:
评论

相关推荐

    Hibernate3.1_学习源码

    配置文件:只配置父类的映射文件,在其中加入joined-subclass将两个子类实体映射关系添加 2) 数据库表:一张表,包括公共字段、特有字段、区分字段 实体层设计:与第一种方法设计一样,设计三个实体类,分父类和...

    hibernate 体系结构与配置 参考文档(html)

    混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...

    HibernateAPI中文版.chm

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...

    hibernate3.2中文文档(chm格式)

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...

    Hibernate3的帮助文档

    10.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 10.1.5. 每个具体类一张表(Table per concrete class) 10.1.6. Table per concrete class, using implicit polymorphism 10.1.7. 隐式多态和其他...

    Hibernate+中文文档

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...

    Hibernate教程

    10.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 10.1.5. 每个具体类一张表(Table per concrete class) 10.1.6. Table per concrete class, using implicit polymorphism 10.1.7. 隐式多态和其他...

    Hibernate中文详细学习文档

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...

    最全Hibernate 参考文档

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...

    Hibernate 中文 html 帮助文档

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...

    hibernate 框架详解

    混合使用“每个类分层结构一张表”和“每个子类一张表” 10.1.5. 每个具体类一张表(Table per concrete class) 10.1.6. Table per concrete class, using implicit polymorphism 10.1.7. 隐式多态和其他继承映射...

    Hibernate3+中文参考文档

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...

    hibernate3.04中文文档.chm

    10.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 10.1.5. 每个具体类一张表(Table per concrete class) 10.1.6. Table per concrete class, using implicit polymorphism 10.1.7. 隐式多态和其他...

    Hibernate参考文档

    9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...

    NHibernate中文帮组文档(2008.11月更新)

    8.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 8.1.5. 每个具体类一张表(Table per concrete class) 8.1.6. 每个具体类一张表使用隐式多态 8.1.7. 隐式多态和其他继承映射混合使用 8.2. 限制 9. ...

    Hibernate注释大全收藏

    • Single Table per Class Hierarchy Strategy: the &lt;subclass&gt; element in Hibernate 每个类层次结构一张表 • Joined Subclass Strategy: the &lt;joined-subclass&gt; element in Hibernate 连接的子类策略 @...

    NHibernate参考文档 2.0.0 chm

    8.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 8.1.5. 每个具体类一张表(Table per concrete class) 8.1.6. 每个具体类一张表使用隐式多态 8.1.7. 隐式多态和其他继承映射混合使用 8.2. 限制 9. ...

    hibernate 教程

    连接的子类(joined-subclass) 5.1.15. map, set, list, bag 5.1.16. 引用(import) 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 持久化枚举...

    NHibernate中文帮助手册API

    连接的子类(joined-subclass)  5.1.15. 联合子类(union-subclass)  5.1.16. 连接  5.1.17. map, set, list, bag  5.1.18. 引用(import)  5.2. NHibernate 的类型  5.2.1. 实体(Entities)和值(values) ...

Global site tag (gtag.js) - Google Analytics