hibernate:继承关系_整个继承树映射到一张表

hibernate继承映射:

domain类:
Employee.java

package com.zhaosoft.domain;

public class Employee {

 private int id;
 private String name;
 private Department 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;
 }

 public Department getDepart() {
  return depart;
 }

 public void setDepart(Department depart) {
  this.depart = depart;
 }

}

两个子类:
sales.java

package com.zhaosoft.domain;

public class Sales extends Employee{

 private int sell;//销售额

 public int getSell() {
  return sell;
 }

 public void setSell(int sell) {
  this.sell = sell;
 }
}
Skiller.java

package com.zhaosoft.domain;

public class Skiller extends Employee{
 private String skill;

 public String getSkill() {
  return skill;
 }

 public void setSkill(String skill) {
  this.skill = skill;
 }
 

}

映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zhaosoft.domain">
     <!-- 当discriminator-value值为0时,为普通员工 -->
 <class name="Employee" discriminator-value="0">  
  <id name="id">
      <!-- 表示主键为自动增长 -->
   <generator class="native"/>
  </id>
  <!-- 鉴别器,区分是那一种子类 -->
 <discriminator column="type" type="int"/>
 <property name="name" type="string"/>
 <many-to-one name="depart" class="Department" column="depart_id"></many-to-one>
 <!-- 当为1时,为技术人员 -->
 <subclass name="Skiller" discriminator-value="1">
    <property name="skill"></property>
 </subclass>
 <!-- 当为2时为销售人员 -->
 <subclass name="Sales" discriminator-value="2">
   <property name="sell"></property>
 </subclass>
 </class>
</hibernate-mapping>
测试:

package com.zhaosoft.test;

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

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zhaosoft.domain.Department;
import com.zhaosoft.domain.Employee;
import com.zhaosoft.domain.Sales;
import com.zhaosoft.domain.Skiller;
import com.zhaosoft.hibernate.HibernateUtil;

public class Many2One {
 public static void main(String[] args) {
  add();
  //queryDepartment(1);
  //queryEmployee(1);
 }

 public static void queryDepartment(int id) {
  Session s = HibernateUtil.getSession();
  try {
   Transaction t = s.beginTransaction();
   t.begin();
   Department de = (Department) s.get(Department.class, 1);
   
   t.commit();
   Hibernate.initialize(de.getEmps());
   System.out.println(de.getName());
   Set<Employee> set=de.getEmps();   //加载数据
   Iterator<Employee> it=set.iterator();
   while(it.hasNext()){
   Employee employee= it.next();
   System.out.println(employee.getName());
   }
   
  } catch (Exception e) {

  } finally {
   if (s != null) {
    s.close();
   }
  }

 }

 public static Employee queryEmployee(int id) {

  Session s = null;
  Transaction tx=null;
  try {

   s=HibernateUtil.getSession();
   tx=s.beginTransaction();
   Employee emp=(Employee)s.get(Employee.class,id);
   //Hibernate.initialize(emp.getDepart());
   tx.commit();
   return emp;
  } finally {
   if (s != null) {
    s.close();
   }
  }

 }

 public static void add() {
  Session s = null;
  Transaction t=null;
  try {
   s=HibernateUtil.getSession();
   t = s.beginTransaction();
   t.begin();
   Department d = new Department();
   d.setName("销售部");

   Employee employee1 = new Employee();
   employee1.setName("小三");
   employee1.setDepart(d);

   Skiller employee2 = new Skiller();
   employee2.setName("李斯");
   employee2.setSkill("skill");
   employee2.setDepart(d);
   
   Sales employee3 = new Sales();
   employee3.setName("王五");
   employee3.setSell(100);
   employee3.setDepart(d);

   Set<Employee> set=new HashSet<Employee>();
   set.add(employee1);
   set.add(employee2);
   set.add(employee3);
   d.setEmps(set);
   s.save(d);
   s.save(employee1);
   s.save(employee2);
   s.save(employee3);
   
   t.commit();
  } catch (Exception e) {

  } finally {
   if (s != null) {
    s.close();
   }
  }
 }
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页