hibernate学习9之extends(继承)映射

hibernate.cfg.xml:

<!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="hibernate.connection.url">jdbc:mysql://localhost/hibernate_extends_1</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bjsxt</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>

<mapping resource="com/bjsxt/hibernate/extends.hbm.xml"/>
</session-factory>
</hibernate-configuration>


假定有2个子类Pig,Bird,他们都继承自Animal

public class Animal {
private int id;
private String name;
private boolean sex;
//setter,getter
}
public class Pig extends Animal {
private int weight;
//setter,getter
}
public class Pig extends Animal {
private int height;
//setter,getter
}

他们的映射关系可以像下面那样,这样可以把Pig 和Bird存在同一张表里,以外键字段type区分存的是Pig还是Bird

<?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="com.bjsxt.hibernate">
<class name="Animal" table="t_animal">
<id name="id">
<generator class="native"/>
</id>
<!--加入鉴别器,用以判断是Pig还是Bird-->
<discriminator column="type" type="string"/>
<property name="name"/>
<property name="sex"/>
<subclass name="Pig" discriminator-value="P">
<property name="weight"/>
</subclass>
<subclass name="Bird" discriminator-value="B">
<property name="height"/>
</subclass>
</class>
</hibernate-mapping>

测试一下:

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import junit.framework.TestCase;

public class ExtendsTest extends TestCase {

public void testSave1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();

Pig pig = new Pig();
pig.setName("猪猪");
pig.setSex(true);
pig.setWeight(100);
session.save(pig);

Bird bird = new Bird();
bird.setName("鸟鸟");
bird.setSex(false);
bird.setHeight(50);
session.save(bird);

session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}

/**
* 采用load,通过Pig查询
*/
public void testLoad1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();

Pig pig = (Pig)session.load(Pig.class, 1);
System.out.println(pig.getName());

session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}

/**
* 采用load,通过Animal查询
*/
public void testLoad2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();

Animal animal = (Animal)session.load(Animal.class, 1);
System.out.println(animal.getName());

session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}

/**
* 采用load,通过Animal查询
*/
public void testLoad3() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();

Animal animal = (Animal)session.load(Animal.class, 1);

//因为load默认只是lazy,因为我们看到的是Animal的代理对象
//所以通过instanceof是反应不出正真的对象类型的
//因此load在默认情况下是不支持多态查询的
if (animal instanceof Pig) {
System.out.println(animal.getName());
}else {
System.out.println("不是猪");
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}

/**
* 采用load,通过Animal查询,将<class>标签上的lazy=false
*/
public void testLoad4() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();

Animal animal = (Animal)session.load(Animal.class, 1);
//可以正确的判断出Pig的类型,因为lazy=false,返回的是具体的Pig类型
//此时load支持多态查询
if (animal instanceof Pig) {
System.out.println(animal.getName());
}else {
System.out.println("不是猪");
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}

/**
* 采用get,通过Animal查询
*/
public void testLoad5() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();

//可以正确的判断出Pig的类型,因为返回的是具体的Pig类型
//get支持多态查询
Animal animal = (Animal)session.get(Animal.class, 1);

if (animal instanceof Pig) {
System.out.println(animal.getName());
}else {
System.out.println("不是猪");
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}

/**
* 采用get,通过Animal查询
*/
public void testLoad6() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
// List animalList = session.createQuery("from Animal").list();
// for (Iterator iter = animalList.iterator(); iter.hasNext();) {
// Animal a = (Animal)iter.next();
// //能够正确的鉴别出正真的类型,hql是支持多态查询的
// if (a instanceof Pig) {
// System.out.println("是Pig");
// }else if (a instanceof Bird) {
// System.out.println("是bird");
// }
// }

List list = session.createQuery("from java.lang.Object").list();
for (Iterator iter=list.iterator(); iter.hasNext();) {
Object o = iter.next();
if (o instanceof Pig) {
System.out.println("是Pig");
}else if (o instanceof Bird) {
System.out.println("是bird");
}
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}

}



多态查询:在hibernate加载数据的时候能鉴别出正真的类型(instanceOf)

get支持多态查询
load只有在lazy=false,才支持多态查询
hql支持多态查询

例外还有一种继承映射关系可以生成3张表(每个类生成一张表,不管是父类还是子类),配置关系如下,不过效率和维护关系比较复杂,不推荐使用。

<?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="com.bjsxt.hibernate">
<class name="Animal" table="t_animal">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="sex"/>
<joined-subclass name="Pig" table="t_pig">
<key column="pid"/>
<property name="weight"/>
</joined-subclass>
<joined-subclass name="Bird" table="t_bird">
<key column="bid"/>
<property name="height"/>
</joined-subclass>
</class>
</hibernate-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值