hibernate 继承映射 joined-subclass

本文介绍了一种避免Hibernate继承映射中大量null值的方法——使用joined-subclass映射方式。这种方式为每个子类创建单独的数据表,演示了如何通过配置实现这种映射,并提供了具体的Java代码及Hibernate XML映射文件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cnblogs.com/zxl/archive/2008/11/13/1332711.html

我在前面写了关于继承映射的整个继承树对应一张表的例子,但是他存在以一定的弊端。在数据表中会产生大量的null值。

为了避免这种情况的发生。我们可以这样去做:

<joined-subclass name="Skiller" table="skiller">
<key column="emp_id"/>
<property name="skill"></property>
</joined-subclass>

<joined-subclass name="Sales" table="sales">
<key column="emp_id"/>
<property name="sell"></property>
</joined-subclass>

通过使用joined-subclass映射,对每一个子类生成一张表。

1、domain类:

package com.zhaosoft.domain;

public class Employee {

private int id;
private String name;
private Department depart;

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 Department getDepart() {
return depart;
}

public void setDepart(Department depart) {
this.depart = depart;
}

}
2、各个子类:

package com.zhaosoft.domain;

public class Sales extends Employee{

private int sell;//销售额

public int getSell() {
return sell;
}

public void setSell(int sell) {
this.sell = sell;
}
}

---------------------------------------------

package com.zhaosoft.domain;

public class Skiller extends Employee{
private String skill;

public String getSkill() {
return skill;
}

public void setSkill(String skill) {
this.skill = skill;
}
}

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.zhaosoft.domain">
<!-- 当discriminator-value值为0时,为普通员工 -->
<class name="Employee" discriminator-value="0">
<id name="id">
<!-- 表示主键为自动增长 -->
<generator class="native"/>
</id>
<property name="name" type="string"/>
<many-to-one name="depart" class="Department" column="depart_id"></many-to-one>

<joined-subclass name="Skiller" table="skiller">
<key column="emp_id"/>
<property name="skill"></property>
</joined-subclass>

<joined-subclass name="Sales" table="sales">
<key column="emp_id"/>
<property name="sell"></property>
</joined-subclass>

</class>

</hibernate-mapping>

4、测试文件:

public static void add() {
Session s = null;
Transaction t=null;
try {
s=HibernateUtil.getSession();
t = s.beginTransaction();
t.begin();
Department d = new Department();
d.setName("销售部");

Employee employee1 = new Employee();
employee1.setName("小三");
employee1.setDepart(d);

Skiller employee2 = new Skiller();
employee2.setName("李斯");
employee2.setSkill("skill");
employee2.setDepart(d);

Sales employee3 = new Sales();
employee3.setName("王五");
employee3.setSell(100);
employee3.setDepart(d);

Set<Employee> set=new HashSet<Employee>();
set.add(employee1);
set.add(employee2);
set.add(employee3);
d.setEmps(set);
s.save(d);
s.save(employee1);
s.save(employee2);
s.save(employee3);

t.commit();
} catch (Exception e) {

} finally {
if (s != null) {
s.close();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值