Hibernate高级映射-继承映射

来源:互联网 发布:php获取radio选中的值 编辑:程序博客网 时间:2024/04/28 02:46

继承映射

继承是面向对象编程中一个很重要的特征,在做面向对象的分析与设计时,经常会设计出具体继承关联的持久化类,如何把这些持久化类之间的继承关系映射到关系数据库上的问题已解决。继承在对象模型和关系模型上是不匹配的。

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

Id name   depart_id  type  skill    sell

                                                 0            null     null

1   Coding   null

2   Null     100

 public class Employee {

    private int id;

    private String name;

    private Department depart;

    //默认的构造方法

    //getter和setter方法

}

public class Skiller extends Employee {

    private String name;

    private String skill;

//默认的构造方法

    //getter和setter方法

}

public class Sales extends Employee {

    private int sell;

//默认的构造方法

    //getter和setter方法

}

映射文件

<classname="Employee" table="employee" discriminator-value="0"><!--unsaved-value="-1"来指定 -->

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

           <generator class="native"/><!-- native主键的生成器  自动增长 -->

       </id>

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

       <property name="name"not-null="true"/><!-- 映射普通的java属性 -->

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

      

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

       <property name="skill"></property>

       </subclass>

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

       <property name="sell"></property>

       </subclass>

    </class>

Type——鉴别器

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

Employee                                  skiller                sales

Id name  depart_id      employee_id skill       employee_id sell

<classname="Employee" table="emplyee">

    <id name="id">

    <generator class="native"></generator>

    </id>

    <property name="name"not-null="true"/><!-- 映射普通的java属性 -->

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

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

       <key column="employee_id"></key>

       <property name="skill"></property>

    </joined-subclass>

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

    <key column="employee_id"/>

    <property name="sell"/>

    </joined-subclass>

    </class>

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

Employee                                                                    type

Id name  depart_id  type skill         employee_id   sell

<classname="Employee" table="employee" discriminator-value="0">

    <id name="id">

    <generator class="native"></generator>

    </id>

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

    <property name="name"not-null="true"/><!-- 映射普通的java属性 -->

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

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

    <property name="skill"/>

    </subclass>

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

    <join table="sales">

    <key column="employee_id"/>

    <property name="sell"/>

    </join>

    </subclass>

    </class>

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

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

    <id name="id">

    <generator class="hilo"/>

    </id>

    <property name="name" not-null="true"/><!-- 映射普通的java属性 -->

    <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"></property>

    </union-subclass>

    </class>

最后:编写测试类TestExtends.java

package com.hbsi.test;

import org.hibernate.Hibernate;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.hbsi.domain.Department;

import com.hbsi.domain.Employee;

import com.hbsi.domain.Sales;

import com.hbsi.domain.Skiller;

import com.hbsi.hibernate.utils.HibernateUtil;

public class TestExtends {

    public static void main(String[] args) {

       // TODO Auto-generatedmethod stub

       add();

       //查询

       System.out.println("-------------查询的结果----------------");

       query(1);

    }

    static void add(){

       Session s=null;

       Transaction tx=null;

       try{

           s=HibernateUtil.getSession();

           tx=s.beginTransaction();

           //增加

           Department depart=new Department();

           depart.setName("departmentone");

          

           Employee emp1=new Employee();

           emp1.setName("aaa");

           emp1.setDepart(depart);

          

           Skiller emp2=new Skiller();

           emp2.setName("bbb");

           emp2.setDepart(depart);

           emp2.setSkill("coding");

          

           Sales emp3=new Sales();

           emp3.setName("ccc");

           emp3.setDepart(depart);

           emp3.setSell(1000);

           //保存

           s.save(emp1);

           s.save(emp2);

           s.save(emp3);

           s.save(depart);

           //提交事务

           tx.commit();

       }finally{

           if(s!=null)

              s.close();

       }

    }

    static void query(int empId){

       Session s=null;

       Transaction tx=null;

       try{

           s=HibernateUtil.getSession();

           tx=s.beginTransaction();

           //查询

           Employee emp=(Employee) s.get(Employee.class,empId);

           System.out.println("员工的姓名:"+emp.getName()+" ,员工类型 :"+emp.getClass());

           if(emp instanceof Skiller){

              ((Skiller) emp).getSkill();

           }else if(emp instanceof Sales){

              ((Sales) emp).getSell();

           }

           //提交

           tx.commit();

       }finally{

           if(s!=null)

              s.close();

       }

    }

}
鸣谢:小薇同学~~~~

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 云南省特岗登录名忘了怎么办 人行登录名忘了再怎么办 生源地贷款登录名忘了怎么办 举报19楼帖子但是不给删除怎么办 新浪微博手机号被注册了怎么办 忘记新浪微博绑定的邮箱账号怎么办 申诉找回微信账号密码失败怎么办 注册微信号时验证码错误怎么办 老板让写的报道没有当天写完怎么办 洛奇英雄传镶嵌有微章的时装怎么办 上午12点用24小时制怎么办 三分钟看懂捷信个人贷款怎么办 精神不正常的父亲到单位闹怎么办 母亲和父亲一直和我闹怎么办 儿子拿了父亲的钱买手机怎么办 电动车骑的慢的时候车头打漂怎么办 二晓啊相公太爱我了怎么办全文免费 车牌被套牌了又有违章怎么办 高中生只学好主课副科学不好怎么办 母泰迪怀孕23天了不吃饭怎么办 媳妇一再触碰我的底线怎么办 发票商品编码好多选错了分类怎么办 吃了用福尔马林泡的食物怎么办 没大没小说话不尊重人的孩子怎么办 升级安卓8.0后app闪退怎么办 业主装门占用消防通道物业怎么办? 想改名字派出所不给改怎么办 物业不给地热打压影响装修怎么办 pos机pin效码验证错误怎么办 苹果下载东西要发验证码怎么办 接口断在了丝扣里面怎么办 政府下了一张关停取缔单怎么办 微信运动数据不刷新了怎么办 邻居霸占我的土地不还我该怎么办 双层水浴式杀菌锅阀门坏了怎么办 通下水管的钢丝断水管里了怎么办 塑料水管与水阀连接处漏水怎么办 介质流向标识管道温度太高怎么办 雪碧里气体太多瓶子涨得要炸怎么办 餐厅不清洗油烟管道物业该怎么办 用完权健洁净宝后奇痒的厉害怎么办