hibernate详解(七)---映射继承关系

Hibernate支持三种继承映射策略

一.采用 subclass 元素的继承映射


备注:school就是下面的sname


  1.建立Person类
package com.eduask.chp.joinsubclass;

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;
}

}


2.子类Student类
 
public class Student extends Person{
private String sname;//学校名称

public String getSname() {
	return sname;
}
public void setSname(String sname) {
	this.sname = sname;
}

}

3.Person.hbm.xml
<hibernate-mapping package="com.eduask.chp.subclass">
    <class name="Person" table="PERSONS" discriminator-value="person1">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <!-- 辨别者列 -->
      <discriminator type="string" column="type"></discriminator>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="age" type="int">
            <column name="AGE" />
        </property>
        <!-- 映射子类Student,使用subclass -->
        <subclass name="Student" discriminator-value="student1">
        <property name="sname" type="string" column="SNAME"></property>
        </subclass>
    </class>
</hibernate-mapping>

二.采用 joined-subclass 元素的继承映射




修改Person.hbm.xml文件
<hibernate-mapping package="com.eduask.chp.subclass">
    <class name="Person" table="PERSONS">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="age" type="int">
            <column name="AGE" />
        </property>
        <joined-subclass name="Student" table="STUDENTS">
        <key column="student_id"></key>
        <property name="sname" type="string" column="SNAME"></property>
        </joined-subclass>
    </class>
</hibernate-mapping>

三.采用 union-subclass 元素的继承映射



Person.hbm.xml
<hibernate-mapping package="com.eduask.chp.union.subclass">
    <class name="Person" table="PERSONS">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="hilo" />
        </id>   
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="age" type="int">
            <column name="AGE" />
        </property>
        <union-subclass name="Student" table="STUDENTS">
        <property name="sname" type="string" column="SNAME"></property>
        </union-subclass>
    </class>
</hibernate-mapping>


三者的区别:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值