Hibernate ORM - 继承关联关系之sub-class

 

  Hibernate对于继承的关联关系有三种处理方式,一种是将整个继承树的持久化实体在一张表中进行映射,一种是将继承树中的每一个持久化实体在数据库中为其各自建立一张表来进行映射,一种是将继承树的子类在数据库中为其各自建立一张表来进行映射。在这里先来看第一种方式的配置方式。

 

  一。Person

 

package com.orm.model;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/19/11
 * Time: 6:13 PM
 */
public abstract class Person extends DomainObject {
    private String name;

    protected Person(String name) {
        this.name = name;
    }
}

 

  二。Male

 

package com.orm.model;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/19/11
 * Time: 6:13 PM
 */
public class Male extends Person {
    private String make;

    public Male(String name, String make) {
        super(name);
        this.make = make;
    }
}

 

  三。Female

 

package com.orm.model;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/19/11
 * Time: 6:13 PM
 */
public class Female extends Person {
    private String maked;

    public Female(String name, String maked) {
        super(name);
        this.maked = maked;
    }
}

 

  四。映射配置文件

 

<?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 default-access="field">

    <class name="com.orm.model.Person" table="person" abstract="true">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <discriminator column="gender" type="java.lang.String"/>
        <property name="name" column="name" type="java.lang.String"/>

        <subclass discriminator-value="male" name="com.orm.model.Male">
           <property name="make" column="make" type="java.lang.String"/>
        </subclass>

        <subclass discriminator-value="female" name="com.orm.model.Female">
             <property name="maked" column="maked" type="java.lang.String"/>
        </subclass>
    </class>

</hibernate-mapping>

 

  五。测试代码

 

package com.orm;

import com.orm.model.Female;
import com.orm.model.Male;
import com.orm.model.Person;
import com.orm.service.CoupleService;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/18/11
 * Time: 3:40 PM
 */
public class HibernateInheritenceTest extends TestCase {
    private CoupleService coupleService;

    @Override
    public void setUp() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:testDataSource.xml");
        coupleService = (CoupleService) context.getBean("coupleService");
    }

    public void testInheritence() throws Exception {
        Person male = new Male("male", "go go go");
        Person female = new Female("female", "yes yes yes");
        coupleService.saveOrUpdate(male);
        coupleService.saveOrUpdate(female);
    }
}

 

  测试结果及截图

 

  

 

  

 

  附上源码以供参考

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值