第一种情况:Hibernate 继承关系每个子类一张表
Person.java
package com.wansha.pojo;
public class Person {
private int id;
private String name;
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;
}
}
Student.java
package com.wansha.pojo;
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.wansha.pojo;
public class Teacher extends Person{
private int salary;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
Teacher.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.wansha.pojo.Teacher" table="teacher">
<id name="id" column="id" type="int">
<generator class="identity"></generator>
</id>
<property name="name" column="name" type="string" />
<property name="salary" column="salary" type="int" />
</class>
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.wansha.pojo.Student" table="student">
<id name="id" column="id" type="int">
<generator class="identity"></generator>
</id>
<property name="name" column="name" type="string" />
<property name="cardId" column="cardId" type="string" />
</class>
</hibernate-mapping>
第二种情况:Hibernate 继承关系所有子类一张表
Person.hbm.xml 只有person.hbm.xml一个配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.wansha.pojo.Person" table="person">
<id name="id" column="id" type="int">
<generator class="identity"></generator>
</id>
<discriminator column="type" type="string" />
<property name="name" column="name" type="string" />
<subclass name="com.wansha.pojo.Student" discriminator-value="student">
<property name="cardId" column="cardId" type="string"/>
</subclass>
<subclass name="com.wansha.pojo.Teacher" discriminator-value="teacher">
<property name="salary" column="salary" type="int"/>
</subclass>
</class>
</hibernate-mapping>
Test.java
package com.wansha.util;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.wansha.pojo.Student;
import com.wansha.pojo.Teacher;
public class Test {
public static void main(String[] args) {
/* Student student = new Student();
student.setName("sharp");
student.setCardId("111111");
Teacher teacher = new Teacher();
teacher.setName("teacher zhang");
teacher.setSalary(800);
Session session = HibernateUtil.getSession();
Transaction ts = null;
try{
ts = session.beginTransaction();
session.save(student);
session.save(teacher);
ts.commit();
}catch(Exception ex){
if(ts != null)ts.rollback();
ex.printStackTrace();
}finally{
HibernateUtil.close(session);
}*/
Session session = HibernateUtil.getSession();
Transaction ts = null;
try{
ts = session.beginTransaction();
List list = session.createQuery("from Person").list();
for(Object object : list){
if(object instanceof Student){
Student student = (Student)object;
System.out.println(student.getCardId());
}else if(object instanceof Teacher){
Teacher teacher = (Teacher)object;
System.out.println(teacher.getSalary());
}
}
ts.commit();
}catch(Exception ex){
if(ts != null)ts.rollback();
ex.printStackTrace();
}finally{
HibernateUtil.close(session);
}
}
}
第三种情况:父类一张表,每个子类一张表,父类的表保存公共有信息,子类的表只保存自己特有的信息
Person.hbm.xml 也是只需要一个配置文件,其它页面与第二种情况一样
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.wansha.pojo.Person" table="person">
<id name="id" column="id" type="int">
<generator class="identity"></generator>
</id>
<property name="name" column="name" type="string" />
<joined-subclass name="com.wansha.pojo.Student" table="student">
<key column="id"/>
<property name="cardId" column="cardId" type="string" />
</joined-subclass>
<joined-subclass name="com.wansha.pojo.Teacher" table="teacher">
<key column="id"/>
<property name="salary" column="salary" type="int" />
</joined-subclass>
</class>
</hibernate-mapping>