继承映射

来源:互联网 发布:克莱汤普森数据 编辑:程序博客网 时间:2024/06/05 02:44

Hibernate为具有继承关系的类也提供了映射的方法,例如,Person类、Employee类和Student类之间具有继承关系。

Person类

package InheritMapping;public class Person {private Long id;private String name;private int age;public Person() {// TODO Auto-generated constructor stub}public Person(Long id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

Employee类

package InheritMapping;public class Employee extends Person {private Long id;private String name;private int age;private double salary;public Employee() {// TODO Auto-generated constructor stub}public Employee(Long id, String name, int age, double salary) {super();this.id = id;this.name = name;this.age = age;this.salary = salary;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}@Overridepublic void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}}

Student类

package InheritMapping;public class Student extends Person {private Long id;private String name;private int age;private String major;public Student() {// TODO Auto-generated constructor stub}public Student(Long id, String name, int age, String major) {super();this.id = id;this.name = name;this.age = age;this.major = major;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getMajor() {return major;}public void setMajor(String major) {this.major = major;}}


1 所有类映射成一张表

Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">     <hibernate-mapping package="InheritMapping">   <!-- 继承映射 (父类:Person(id,name,age),Employee(salary) 和 Student(major) 为其子类)--><!-- 1、所有类映射成一张表discriminator 判别者--><class name="Person" table="person" discriminator-value="INHERITPERSON" > <id name="id" type="java.lang.Long"><column name="id" /><generator class="identity"/></id><discriminator column="person_type" type="string" /><property name="name" type="string" column="person_name" /><property name="age" type="integer" column="person_age"/><subclass name="Employee" discriminator-value="EMP"><property name="salary" type="double"><column name="salary" /></property></subclass><subclass name="Student" discriminator-value="STUD"><property name="major" type="java.lang.String"><column name="major" /></property></subclass></class></hibernate-mapping>


测试

package InheritMapping;import org.hibernate.Session;import org.hibernate.Transaction;import util.HibernateUtil;public class test {public static void main(String[] args) {// TODO Auto-generated method stubSession session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();InheritPerson p = new InheritPerson(new Long(101),"王小明",25);InheritStudent stud = new InheritStudent(new Long(102),"李大海",23,"计算机科学");InheritEmployee emp = new InheritEmployee(new Long(103),"刘明",24,3800);session.save(p);session.save(stud);session.save(emp);tx.commit();}}

结果



2 每个子类映射成一张表

Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">            <hibernate-mapping package="InheritMapping">   <!-- 继承映射 (父类:Person(id,name,age),Employee(salary) 和 Student(major) 为其子类)--><!-- 2、每个子类映射成一张表(对于子类中属于父类的属性存储在父类表中) --><class name="Person" table="person2" > <id name="id" type="java.lang.Long"><column name="person_id" /><generator class="identity"/></id><property name="name" type="string" column="person_name" /><property name="age" type="integer" column="person_age"/><joined-subclass name="Employee" table="employee" ><key column="person_id" /><property name="salary" type="double"><column name="salary" /></property></joined-subclass><joined-subclass name="Student" table="student"><key column="person_id" /><property name="major" type="java.lang.String"><column name="major" /></property></joined-subclass></class></hibernate-mapping>

测试代码同1一样

运行结果



3 每个具体类映射成一张表

Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">     <hibernate-mapping package="InheritMapping">   <!-- 继承映射 (父类:Person(id,name,age),Employee(salary) 和 Student(major) 为其子类)--><!-- 3、每个具体类映射成一张表 --><class name="Person" abstract="true" > <id name="id" type="java.lang.Long" column="person_id" ><generator class="assigned"/></id><property name="name" type="string" column="person_name" length="20" /><property name="age" type="integer" column="person_age"/><union-subclass name="Employee" table="employee" ><property name="salary" type="double"><column name="salary" /></property></union-subclass><union-subclass name="Student" table="student"><property name="major" type="java.lang.String"><column name="major" length="30" /></property></union-subclass></class></hibernate-mapping>

测试

package InheritMapping;import org.hibernate.Session;import org.hibernate.Transaction;import util.HibernateUtil;public class test {public static void main(String[] args) {// TODO Auto-generated method stubSession session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();InheritStudent stud = new InheritStudent(new Long(102),"李大海",23,"计算机科学");InheritStudent stud2 = new InheritStudent(new Long(101),"王小明",22,"电子商务");InheritEmployee emp = new InheritEmployee(new Long(301),"刘明",24,3800);session.save(stud2);session.save(stud);session.save(emp);tx.commit();}}

结果


原创粉丝点击