继承关系分为三种:一张表对应一整棵类继承树、一个类对应一张表、每一个具体类对应一张表。
三个java类Person、Student、Teacher,Student和Teacher继承Person类并且Student和Teacher没有任何关系
代码如下:
Person.java
package com.zzn.hibernate.model;
public class Person {
private int id;
private String name;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student.java
package com.zzn.hibernate.model;
public class Student extends Person {
private String cardId;
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
}
Teacher.java
package com.zzn.hibernate.model;
public class Teacher extends Person {
private int salary;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
一张表对应一整棵类继承树时
Person.hbm.xml
<?xml version="1.0" encoding='gb2312'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zzn.hibernate.model">
<class name="Person" table="person">
<id name="id" column="id">
<generator class="identity" />
</id>
<discriminator column="personType" type="java.lang.String" /> <!-- 指定描述符对应的列明,注意discrimination标签只能紧跟在id标签后面 -->
<property name="name" type="java.lang.String" /> <!-- 公用的name属性,被两个sbuclass标签所用 -->
<property name="age" type="java.lang.Integer" /> <!-- 公用的age属性,被两个sbuclass标签所用 -->
<subclass name="Student" discriminator-value="student"> <!-- 映射子类Student,描述符是student,此时已经映射Student类,所以不必再写Student.hbm.xml -->
<property name="cardId" type="java.lang.String" /> <!-- Student私有的属性 -->
</subclass>
<subclass name="Teacher" discriminator-value="teacher" >
<property name="salary" type="java.lang.Integer" /> <!-- Teacher私有的属性 -->
</subclass>
</class>
</hibernate-mapping>
一个类对应一张表
<?xml version="1.0" encoding='gb2312'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zzn.hibernate.model">
<class name="Person" table="person">
<id name="id" column="id">
<generator class="identity" />
</id>
<property name="name" type="java.lang.String" />
<property name="age" type="java.lang.Integer" />
<joined-subclass name="Student" table="student">
<key column="id" />
<property name="cardId" type="java.lang.String" />
</joined-subclass>
<joined-subclass name="Teacher" table="teacher">
<key column="id"/>
<property name="salary" type="java.lang.Integer" />
</joined-subclass>
</class>
</hibernate-mapping>
hibernate.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;databasename=hibernate_test</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="myeclipse.connection.profile">SQL2005</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<mapping resource="com/zzn/hibernate/model/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试文件SubclassTest.java
package com.zzn.hibernate.test;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zzn.hibernate.model.Person;
import com.zzn.hibernate.model.Student;
import com.zzn.hibernate.model.Teacher;
public class SubClassTest {
public static void main(String[] args) {
add();
select();
}
public static void add() {
Configuration configuration = null;
SessionFactory sessionFactory = null;
Session session = null;
Transaction transaction = null;
Student student = new Student();
Teacher teacher = new Teacher();
try {
configuration = new Configuration();
sessionFactory = configuration.configure().buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
student.setName("shanshan");
student.setAge(25);
student.setCardId("123456");
teacher.setName("xiaolan");
teacher.setAge(25);
teacher.setSalary(5000);
session.save(student);
session.save(teacher);
} finally {
if (session != null) {
transaction.commit();
session.close();
}
}
}
public static void select(){
Configuration configuration = null;
SessionFactory sessionFactory = null;
Session session = null;
try {
configuration = new Configuration();
sessionFactory = configuration.configure().buildSessionFactory();
session = sessionFactory.openSession();
Query query = session.createQuery("from Person");
Iterator iterator = query.list().iterator();
while (iterator.hasNext()){
Person person = (Person)iterator.next();
System.out.println(person.getName());
}
} finally {
if (session != null) {
session.close();
}
}
}
}