Employee

public class Employee {
private Long id;
private String name;

HourlyEmployee

public class HourlyEmployee extends Employee {
private Double rate;

SalariedEmployee

public class SalariedEmployee extends Employee {
private Double salary;

Employee.hbm.xml

<class name="Employee" table="t_employee" discriminator-value="1">
<id name="id">
<generator class="native" />
</id>
<discriminator column="types" type="int"/>
<property name="name" />
<!-- 在配置子类 -->
<subclass name="HourlyEmployee" discriminator-value="2">
<join table="t_HourlyEmployee">
<key column="id" />
<property name="rate" />
</join>
</subclass>
<subclass name="SalariedEmployee" discriminator-value="3">
<join table="t_SalariedEmployee">
<key column="id" />
<property name="salary" />
</join>
</subclass>
</class>

MainTest

public class MainTest {
Session session = null;
@Before
public void save() {
session = HibernateUtils.INSTANCE.getSession();
session.beginTransaction(); Employee employee = new Employee("jege"); HourlyEmployee hourlyEmployee = new HourlyEmployee();
hourlyEmployee.setName("hourly");
hourlyEmployee.setRate(200D); SalariedEmployee salariedEmployee = new SalariedEmployee();
salariedEmployee.setName("salaried");
salariedEmployee.setSalary(300D); session.save(employee);
session.save(hourlyEmployee);
session.save(salariedEmployee);
} @Test
public void get() {
Employee employee = (Employee) session.get(Employee.class, 1L);
System.out.println(employee.getName()); HourlyEmployee hourlyEmployee = (HourlyEmployee) session.get(HourlyEmployee.class, 2L);
System.out.println(hourlyEmployee.getName());
System.out.println(hourlyEmployee.getRate()); SalariedEmployee salariedEmployee = (SalariedEmployee) session.get(SalariedEmployee.class, 3L);
System.out.println(salariedEmployee.getName());
System.out.println(salariedEmployee.getSalary());
} @After
public void colse() {
session.getTransaction().commit();
session.close();
}
}

源码地址

https://github.com/je-ge/hibernate

如果觉得我的文章对您有帮助,请予以打赏。您的支持将鼓励我继续创作!谢谢!



Hibernate 系列教程13-继承-鉴别器与内连接相结合的更多相关文章

  1. Hibernate 系列教程11-继承-Single Table策略

    Single Table策略 通过 discriminator鉴别器来区分是父类还是子类 Employee public class Employee { private Long id; priva ...

  2. Hibernate 系列教程17-查询缓存

    在二级缓存配置成功的基础上进行查询缓存配置 Product public class Product { private Long id; private String name; Product.h ...

  3. Hibernate 系列教程16-二级缓存

    pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate- ...

  4. Hibernate 系列教程15-一级缓存

    Product public class Product { private Long id; private String name; Product.hbm.xml <class name= ...

  5. Hibernate 系列教程14-继承-PerTable策略

    Employee public class Employee { private Long id; private String name; HourlyEmployee public class H ...

  6. Hibernate 系列教程12-继承-Join策略

    Employee public class Employee { private Long id; private String name; HourlyEmployee public class H ...

  7. Hibernate 系列教程10-组成关系

    组成关系 在一个员工模型里面需要存入 员工公司所在地址的城市,街道 员工籍贯所在的城市,街道, 此时可以抽取城市,街道变成一个模型即是组成关系 Employee public class Employ ...

  8. Hibernate 系列教程9-自关联

    自关联:本质还是原来双向一对多,原来要配置两个类,现在全部都配置在一个类里面 Employee public class Employee { private Long id; private Str ...

  9. Hibernate 系列教程8-复合主键

    复合主键 复合主键的意思就是2个字段同时为主键 不使用无业务含义的自增id作为主键 Airline package com.jege.hibernate.compositeid; import jav ...

随机推荐

  1. MongoDB高级操作

    参考MongoDB菜鸟教程 一.$type操作符 MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1   String 2   Object 3   Array 4   ...

  2. webpack-hot-middleware 用于 livereload

    https://github.com/glenjamin/webpack-hot-middleware Webpack hot reloading using only webpack-dev-mid ...

  3. JS学习之路,之弹性运动框架

    弹性运动:顾名思义,就如同物理中的加速减速运动,当开始时速度过大,到达终点时,速度不会立刻停下,而是再前进一段距离,而后再向相反方向运动,如此往复. var timer=null; var speed ...

  4. ZIP文件解压

    public class DZip { /// <summary> /// 压缩为ZIP文件 /// </summary> public void Zip(string dir ...

  5. AltiumDesigner14绘制四层板设置

    1,快捷键(O+K)进入板层设置界面: 2,选择AddLayer,里边有两个选项(add layer(添加信号层)||add internal plane(增加平面))  四层板的话一般层次的划分是t ...

  6. ACPI

    高级配置与电源接口(Advanced Configuration and Power Interface),简称ACPI.1997年由Intel.Microsoft.Toshiba 所共同制定提供操作 ...

  7. ASP.NET中application对象的用法(面试题)

    ASP.NET中application对象的用法 本文导读:Application对象是HttpApplicationState类的一个实例,Application状态是整个应用程序全局的.Appli ...

  8. 美化type="file"控件

    对于type="file"而言是一个不大好看的控件.如果不美化他一下,总感觉自己的网站有些缺乏了美感 上代码: <!DOCTYPE html> <html> ...

  9. jquery源码阅读(1)

    每天坚持阅读一定量的的jquery代码,积少成多!加油加油! jquery-2.2.1的9161~9194行 1 if ( typeof define === "function" ...

  10. VBS操作JS网页元素实例

    '=========================================================================='' VBScript Source File - ...