字段名 | 主键 | 类型 | 描述 |
id | pk | int | 员工编号 |
name | varchar(50) | 员工姓名 | |
department_id | fk | int | 所属部门编号 |
type | int | 所属员工类型 | |
skill | varchar(50) | 技能员工(type=1) | |
sell | int | 销售员工(type=2) |
package domain; import java.util.Set; public class Department { private int id; private String name; private Set<Employee> emps; 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 Set<Employee> getEmps() { return emps; } public void setEmps(Set<Employee> emps) { this.emps = emps; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain"> <class name="Department" table="`department`"> <id name="id" column="`id`"> <generator class="native"></generator> </id> <properties name="name"></properties> <set name="emps"> <key column="depart_id"></key> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
package domain; public class Employee { private int id; private String name; private Department department; public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } 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; } }
package domain; public class Sales extends Employee { private int sell; public int getSell() { return sell; } public void setSell(int sell) { this.sell = sell; } }
package 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 SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain"> <class name="Employee" table="`employee`" discriminator-value="0"> <id name="id" column="`id`"> <generator class="native"></generator> </id> <discriminator column="type" type="int"></discriminator> <properties name="name"></properties> <many-to-one name="department" column="depart_id"></many-to-one> <subclass name="Skiller" discriminator-value="1"> <property name="skill"></property> </subclass> <subclass name="Sales" discriminator-value="2"> <property name="sell"></property> </subclass> </class> </hibernate-mapping>
测试:
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(); } } } }