采用<union-subclass>元素的继承映射

在这个方式下,父类实例的胡数据保存在父表中,而子类实例的数据则保存在子表中,子类表的字段会比父类表字段要多,因为子类的字段等于父类属性加子类增加属性的综合。




这种映射策略的主键生成方式需要设置为“hilo”,因为多个持久化实体对应的记录的主键是连续的

以下是测试代码:

public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
Transaction transaction=session.beginTransaction();
People p=new People();

Address address_1=new Address("天河","434333","中国");
Address address_2=new Address("杭州","123456","中国");
Address address_3=new Address("温州","123456","中国");
Customer customer_1=new Customer("喜欢购物");
Customer customer_2=new Customer("喜欢吃");
Employee employ_1=new Employee("项目经理",12000);
Employee employ_2=new Employee("项目组长",4500);
Manager manager=new Manager("人力资源");
manager.setTitle("管理员");
manager.setSalary(15000);
manager.setAddress(address_3);
manager.setName("ciciwu6");
manager.setGender('女');
p.setAddress(address_1);
p.setName("ciciwu1");
p.setGender('男');
employ_1.setManager(manager);
employ_1.setAddress(address_2);
employ_1.setName("ciciwu4");
employ_1.setGender('女');
employ_2.setManager(manager);
employ_2.setAddress(address_3);
employ_2.setName("ciciwu5");
employ_2.setGender('女');
customer_1.setAddress(address_2);
customer_1.setName("ciciwu2");
customer_1.setGender('女');
customer_1.setEmployee(employ_1);
customer_2.setAddress(address_3);
customer_2.setName("ciciwu3");
customer_2.setGender('男');
customer_2.setEmployee(employ_1);
System.out.println("print");
session.save(p);
session.save(manager);
session.save(employ_1);
session.save(employ_2);
session.save(customer_1);
session.save(customer_2);
transaction.commit();
session.close();
sf.close();
}

以下是各实体类:

package model;


public class People {
private Integer id;
private String name;
private char gender;
private Address address;
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 char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}

}

/

package model;


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


public class Employee extends People{
private String title;
private double salary;
private Set<Customer> customers=new HashSet<Customer>();
private Manager manager;
public Employee(){
}
public Employee(String title,double salary){
this.salary=salary;
this.title=title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Set<Customer> getCustomers() {
return customers;
}
public void setCustomers(Set<Customer> customers) {
this.customers = customers;
}
public Manager getManager() {
return manager;
}
public void setManager(Manager manager) {
this.manager = manager;
}

}

/

package model;


public class Address {
private String detail;
private String zip;
private String country;
public Address(){

}
public Address(String detail,String zip,String country){
this.detail=detail;
this.country=country;
this.zip=zip;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}

}

/

package model;


public class Customer extends People{
private String comments;
private Employee employee;
public Customer(){

}
public Customer(String comments){
this.comments=comments;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}

}

/

package model;


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


public class Manager extends Employee {
private String department;
private Set<Employee> employees=new HashSet<Employee>();
public Manager(){

}
public Manager(String department){
this.department=department;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}

}

以下是数据库中的表结果:

people:


manager:


employee:


customer:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值