欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

hibernate 映射继承关系

程序员文章站 2022-04-28 18:53:29
...

实现方式一般有三种: 1. 继承关系树每个具体类对应一张表(不介绍) 2. 继承关系树的根类对应一张表 3. 继承关系树的每个类对应一张表 先介绍关系: DayEmployee和MonthEmploy是Employee的子类,并且Company和Employee是一对多关系: 具体代码如下: Compan

实现方式一般有三种:
1. 继承关系树每个具体类对应一张表(不介绍)
2. 继承关系树的根类对应一张表
3. 继承关系树的每个类对应一张表

先介绍关系:
DayEmployee和MonthEmploy是Employee的子类,并且Company和Employee是一对多关系:

具体代码如下:

Company.java

import java.util.HashSet;
import java.util.Set;

public class Company {
    private Integer id;
    private String name;
    private Set employees = new HashSet();

    public Company() {
        super();
    }
    public Company(String name) {
        super();
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public SetgetEmployees() {
        return employees;
    }
    public void setEmployees(Set employees) {
        this.employees = employees;
    }
}

Employee.java

    public class Employee {
    private Integer id;
    private String name;
    private Integer age;
    private Company company;
    public Company getCompany() {
        return company;
    }
    public void setCompany(Company company) {
        this.company = company;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

}

DayEmployee.java

public class DayEmployee extends Employee{
    private Double dayMoney;

    public Double getDayMoney() {
        return dayMoney;
    }
    public void setDayMoney(Double dayMoney) {
        this.dayMoney = dayMoney;
    }

}

MonthEmploy.java

    public class MonthEmploy extends Employee{
    private Double monthMoney;

    public Double getMonthMoney() {
        return monthMoney;
    }
    public void setMonthMoney(Double monthMoney) {
        this.monthMoney = monthMoney;
    }

}

Company.hbm.xml



hibernate-mapping package="com.dongecs.test1.pojo">
    class name="Company" table="COMPANY">