我在前面写了关于继承映射的整个继承树对应一张表的例子,但是他存在以一定的弊端。在数据表中会产生大量的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();
}
}
}
分享到:
相关推荐
配置文件:只配置父类的映射文件,在其中加入joined-subclass将两个子类实体映射关系添加 2) 数据库表:一张表,包括公共字段、特有字段、区分字段 实体层设计:与第一种方法设计一样,设计三个实体类,分父类和...
混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...
10.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 10.1.5. 每个具体类一张表(Table per concrete class) 10.1.6. Table per concrete class, using implicit polymorphism 10.1.7. 隐式多态和其他...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...
10.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 10.1.5. 每个具体类一张表(Table per concrete class) 10.1.6. Table per concrete class, using implicit polymorphism 10.1.7. 隐式多态和其他...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...
混合使用“每个类分层结构一张表”和“每个子类一张表” 10.1.5. 每个具体类一张表(Table per concrete class) 10.1.6. Table per concrete class, using implicit polymorphism 10.1.7. 隐式多态和其他继承映射...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...
10.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 10.1.5. 每个具体类一张表(Table per concrete class) 10.1.6. Table per concrete class, using implicit polymorphism 10.1.7. 隐式多态和其他...
9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射...
8.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 8.1.5. 每个具体类一张表(Table per concrete class) 8.1.6. 每个具体类一张表使用隐式多态 8.1.7. 隐式多态和其他继承映射混合使用 8.2. 限制 9. ...
• Single Table per Class Hierarchy Strategy: the <subclass> element in Hibernate 每个类层次结构一张表 • Joined Subclass Strategy: the <joined-subclass> element in Hibernate 连接的子类策略 @...
8.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 8.1.5. 每个具体类一张表(Table per concrete class) 8.1.6. 每个具体类一张表使用隐式多态 8.1.7. 隐式多态和其他继承映射混合使用 8.2. 限制 9. ...
连接的子类(joined-subclass) 5.1.15. map, set, list, bag 5.1.16. 引用(import) 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 持久化枚举...
连接的子类(joined-subclass) 5.1.15. 联合子类(union-subclass) 5.1.16. 连接 5.1.17. map, set, list, bag 5.1.18. 引用(import) 5.2. NHibernate 的类型 5.2.1. 实体(Entities)和值(values) ...