Hibernate继承映射

 

Hibernate继承映射

继承是面向对象编程中一个很重要的特征,在做面向对象的分析与设计是,经常会设计出具体继承关联的持久化类。


持久化类

 

Employee类

public class Employee {

    private int id;

    private String name;

    private Department depart;

    public Department getDepart() {

       return depart;

    }

    public void setDepart(Department depart) {

       this.depart = 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;

    }

   

}

Skiller类

public class Skiller extends Employee {

    private String skill;

 

    public String getSkill() {

       return skill;

    }

 

    public void setSkill(String skill) {

       this.skill = skill;

    }

   

}

Sales类

public class Sales extends Employee {

    private int sell;

 

    public int getSell() {

       return sell;

    }

 

    public void setSell(int sell) {

       this.sell = sell;

    }

   

}

方式一:整个的继承体系就用一张表。设计一张表employee

Employee.hbm.xml文件

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping

    package="com.hbsi.domain">

 

<class name="Employee" table="employee" discriminator-value="0">

       <id name="id" column="id">

           <generator class="native"/>

       </id>

       <discriminator column="type" type="int"/>

       <property name="name" column="name"/>

       <many-to-one name="depart" column="depart_id"/>

      

       <subclass name="Skiller" discriminator-value="1">

           <property name="skill"/>

       </subclass>

       <subclass name="Sales" discriminator-value="2">

           <property name="sell"/>

       </subclass>

    </class>

</hibernate-mapping>

方式二:每个子类一张表,存放子类所特有的属性

Employee.hbm.xml文件

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping

    package="com.hbsi.domain">

<class name="Employee" table="employee">

       <id name="id">

           <generator class="native"/>

       </id>

       <property name="name" column="name"/>

       <many-to-one name="depart" column="depart_id"/>

       <joined-subclass name="Skiller" table="skiller">

           <key column="employee_id"/>

           <property name="skill"/>

       </joined-subclass>

       <joined-subclass name="Sales" table="sales">

           <key column="employee_id"/>

           <property name="sell"/>

       </joined-subclass>

    </class>

</hibernate-mapping>

方式三:混合使用“一个类继承体系一张表”和“每个子类一张表”

Employee.hbm.xml文件

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping

    package="com.hbsi.domain">

 

    <class name="Employee" table="employee" discriminator-value="0">

       <id name="id">

           <generator class="native"/>

       </id>

       <discriminator column="type" type="int" />

       <property name="name" column="name"/>

       <many-to-one name="depart" column="depart_id"/>

       <subclass name="skiller" discriminator-value="1">

           <property name="skill"/>

       </subclass>

       <subclass>

           <join table="sales">

              <key column="employee_id"/>

              <property name="sell"/>

           </join>

       </subclass>

    </class>

</hibernate-mapping>

方式四:每个具体类一张表(union-subclass) ,保存是子类完整信息

Employee.hbm.xml文件

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping

    package="com.hbsi.domain">

<class name="Employee" table="employee">

       <id name="id">

           <generator class="hilo"/>

       </id>

       <property name="name" column="name"/>

       <many-to-one name="depart" column="depart_id"/>

       <union-subclass name="Skiller" table="skiller">

           <property name="skill"/>

       </union-subclass>

       <union-subclass name="Sales" table="sales">

           <property name="sell"/>

       </union-subclass>

    </class>

</hibernate-mapping>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值