Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

Hibernate继承映射

来源: liu716zhifei 分享至:
 

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>

 


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史