首页 > 代码库 > hibernate中的继承关系
hibernate中的继承关系
- 1. hibernate中的继承关系
代码;
<1>、POJO类:(Person类)
package qau.edu;
import java.util.Date;
public class Person {
// 成员变量;
private int id ; private String name ; private Date date ;
// 相应的getter和setter方法;
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 Date getDate() { return date; } public void setDate(Date date) { this.date = date; }
}
|
(Teacher类)
package qau.edu;
public class Teacher extends Person {
// 成员变量;
private String job ;
// 相应的getter和setter 方法;
public void setJob(String job){
this.job = job ; } public String getJob(){
return job ; } }
|
<2>、关系映射文件;
(Person)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="qau.edu">
<class name="Person" table="t_person" discriminator-value=http://www.mamicode.com/"Person">
<id name="id" column="t_id"> <generator class="native"/> </id>
<!-- 配置辨别项 -->
<discriminator column="TYPE"/>
<property name="name" column="t_name"/> <property name="date" column="t_date"/>
<!-- 配置继承关系 -->
<subclass name="Teacher" discriminator-value=http://www.mamicode.com/"Teacher"> <property name="job" column="T_job"/> </subclass>
</class>
</hibernate-mapping>
|
<3>、测试类;(进行相关的保存和查询的操作)
package qau.edu;
import java.util.Date;
import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test;
public class RenTest {
private Session session ; private Transaction tr ;
@Before public void init(){
// 打开Session ;
session = new Configuration().configure().buildSessionFactory().openSession();
// 开启事务;
tr = session.beginTransaction();
}
@After public void destroy(){
// 提交事务;
tr.commit();
// 关闭会话;
session.close(); }
// 进行保存操作;
@Test public void testSave() {
// 实例化对象;
Person per = new Person();
// 设置属性值;
per.setName("AAA"); per.setDate(new Date());
// 实例化Teacher对象;
Teacher teacher = new Teacher();
// 设置属性值;
teacher.setName("BB"); teacher.setDate(new Date()); teacher.setJob("Teacher");
// 保存对象;
session.save(per); session.save(teacher);
}
// 进行查询操作;
@Test public void testGet(){
// 获取对象;
Person per = (Person)session.get(Person.class, 1);
// 输出;
System.out.println("名字是:" + per.getName());
// 获取teacher对象;
Teacher ter = (Teacher)session.get(Teacher.class, 2);
// 输出:
System.out.println("工作是:" + ter.getJob()); }
} |
执行结果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Hibernate: select person0_.t_id as t1_0_0_, person0_.t_name as t3_0_0_, person0_.t_date as t4_0_0_, person0_.T_job as T5_0_0_, person0_.TYPE as TYPE0_0_ from t_person person0_ where person0_.t_id=? 名字是:AAA Hibernate: select teacher0_.t_id as t1_0_0_, teacher0_.t_name as t3_0_0_, teacher0_.t_date as t4_0_0_, teacher0_.T_job as T5_0_0_ from t_person teacher0_ where teacher0_.t_id=? and teacher0_.TYPE=‘Teacher‘ 工作是:Teacher Hibernate: insert into t_person (t_name, t_date, TYPE) values (?, ?, ‘Person‘) Hibernate: insert into t_person (t_name, t_date, T_job, TYPE) values (?, ?, ?, ‘Teacher‘)
|
另一种方法:
代码:
映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="qau.edu">
<class name="Person" table="t_person">
<id name="id" column="t_id"> <generator class="native"/> </id>
<property name="name" column="t_name"/> <property name="date" column="t_date"/>
<!-- 配置继承关系 -->
<joined-subclass name="Teacher">
<key column="T_Id"/> <property name="job"/> </joined-subclass>
</class>
</hibernate-mapping>
|
其他的代码相同:
执行结果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Hibernate: insert into t_person (t_name, t_date) values (?, ?) Hibernate: insert into t_person (t_name, t_date) values (?, ?) Hibernate: insert into Teacher (job, T_Id) values (?, ?)
|
通过上面知道:
第一种方式也就是<subclass>时候进行插入的时候是在一张表中进行的,但是在第二种的时候却是:
进行插入父类数据的时候是只在父表中进行的,但是当进行子类数据的插入时候却是先进行的父表的插入,然乎再进行的子类表的插入,因为第一种方式只是生成了一张表,第二种则是生成了两个表。
hibernate中的继承关系