Hibernate的继承映射可以理解两个持久化类之间的继承关系,例如来说和人之间的关系,来说继承了人,可以认为来说是一个特殊的人,如果对人进行查询,老师的实例也将被得到。各个类的代码如下:
package com.pengsuen.bean;
public class Person
{
private long id;
private String name;
private char gender;
private Address address;
public Person()
{
}
public Person(long id , String name , char gender)
{
this.id = id;
this.name = name;
this.gender = gender;
}
public void setId(long id)
{
this.id = id;
}
public long getId()
{
return this.id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setGender(char gender)
{
this.gender = gender;
}
public char getGender()
{
return this.gender;
}
public void setAddress(Address address)
{
this.address = address;
}
public Address getAddress()
{
return this.address;
}
}
package com.pengsuen.bean;
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.zip = zip;
this.country = country;
}
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 com.pengsuen.bean;
public class Customer extends Person{
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 com.pengsuen.bean;
import java.util.HashSet;
import java.util.Set;
public class Employee extends Person{
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.title = title;
this.salary = salary;
}
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 com.pengsuen.bean;
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;
}
}
在这种映射策略下,整个继承树的所有实例都将保存在同一个表内,因此,需要在该表中额外增加一列,使用该列来区分每行记录到底是那个类的实例--这个列被称为辨别者列(discriminator),配置文件如下:
<?xml version="1.0" encoding="utf8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.pengsuen.bean">
<class name="Person" discriminator-value="Person">
<id name="id" column="person_id">
<generator class="increment"/>
</id>
<discriminator column="wawa" type="string"/>
<property name="name" length="80"/>
<property name="gender"/>
<component name="address">
<property name="detail"/>
<property name="zip"/>
<property name="country"/>
</component>
<subclass name="Employee" discriminator-value="Employee">
<property name="title" />
<property name="salary" />
<many-to-one name="manager" column="manager_id"/>
<set name="customers" inverse="true">
<key column="empoyee_id"/>
<one-to-many class="Customer"/>
</set>
<subclass name="Manager" discriminator-value="Manager">
<property name="department"/>
<set name="employees" inverse="true">
<key column="manager_id"/>
<one-to-many class="Employee"/>
</set>
</subclass>
</subclass>
<subclass name="Customer" discriminator-value="Customer">
<property name="comments"/>
<many-to-one name="employee" column="empoyee_id"/>
</subclass>
</class>
</hibernate-mapping>
总配置文件hibernate.cfg.xml略
测试代码如下:
package com.pengsuen.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.pengsuen.bean.Address;
import com.pengsuen.bean.Customer;
import com.pengsuen.bean.Employee;
import com.pengsuen.bean.Manager;
import com.pengsuen.bean.Person;
public class Test {
public static void main(String[] args) {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Employee employee1 = new Employee();
employee1.setName("Sean");
employee1.setTitle("Coder");
employee1.setGender('M');
employee1.setSalary(4500);
employee1.setAddress(new Address("NYC","123456","USA"));
Employee employee2 = new Employee();
employee2.setName("Peter");
employee2.setTitle("Coder");
employee2.setGender('M');
employee2.setSalary(4500);
employee2.setAddress(new Address("Beijing","123456","China"));
Manager grace = new Manager();
grace.setName("Grace");
grace.setTitle("Manager");
grace.setGender('F');
grace.setSalary(12000);
grace.setAddress(new Address("CA","523034","USA"));
grace.setDepartment("Si");
employee2.setManager(grace);
Customer customer1 = new Customer();
customer1.setName("Mike");
customer1.setGender('M');
customer1.setAddress(new Address("Beijing","233034","China"));
customer1.setComments("kfljsdfkjasklfjkasld");
customer1.setEmployee(grace);
Person lee = new Person();
lee.setName("Yeeku");
lee.setGender('M');
lee.setAddress(new Address("Beijing","434333","China"));
session.save(lee);
session.save(grace);
session.persist(employee1);
session.persist(employee2);
session.save(customer1);
tx.commit();
session.close();
sf.close();
}
}