继承关系(一张表)
employee字段名主键类型描述idpkint员工编号name?varchar(50)员工姓名department_idfkint所属部门编号type?int所属员工类型skill?varchar(50)技能员工(type=1)sell?int销售员工(type=2)
?
测试:
package test;import java.util.HashSet;import java.util.Set;import org.hibernate.Transaction;import org.hibernate.classic.Session;import dao.HibernateUtil;import domain.Department;import domain.Employee;import domain.Sales;import domain.Skiller;public class Extends {public static void main(String[] args) {add();}static void add(){Session session = null;Transaction tx = null;try {Department depart = new Department();depart.setName("depart name 1");Employee emp1 = new Employee();emp1.setName("emp1");emp1.setDepartment(depart);Skiller emp2 = new Skiller();emp2.setName("emp2");emp2.setSkill("skill");emp2.setDepartment(depart); Sales emp3 = new Sales();emp3.setName("emp3");emp3.setDepartment(depart);emp3.setSell(100);Set<Employee> emps = new HashSet<Employee>();emps.add(emp1);emps.add(emp2);emps.add(emp3);depart.setEmps(emps);session = (Session) HibernateUtil.getSession();tx = session.beginTransaction();session.save(depart);session.save(emps);tx.commit();} catch (Exception e) {// TODO: handle exception}finally{if(session != null){session.close();}}}}?