20160509-hibernate--继承映射


<class name="Employee" table="employee" discriminator-value="0">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="type" type="int"/>
<property name="name"/>
<many-to-one name=”depart” column=”depart_id”/>
<subclass name="Skiller" discriminator-value="1">
<property name=”skill”/>
</subclass>
<subclass name="Sales" discriminator-value="2">
<property name="sell"/>
</subclass>
</class>
package com.dzq.domain;
import java.io.Serializable;
public class Employee implements Serializable{
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;
}
}
子类:
package com.dzq.domain;
public class Sales extends Employee {
private int sell;
public int getSell() {
return sell;
}
public void setSell(int sell) {
this.sell = sell;
}
}
package com.dzq.domain;
public class Skiller extends Employee {
private String skiller;
public String getSkiller() {
return skiller;
}
public void setSkiller(String skiller) {
this.skiller = skiller;
}
}
映射关系:
<?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.dzq.domain"> <class name="Employee" table="employee" discriminator-value="0">
<id name="id" column="id">
<generator class="native" />
</id>
<discriminator column="type"/>
<property name="name" column="name" />
<many-to-one name="depart" column="depart_id" />
<subclass name="Skiller" discriminator-value="1">
<property name="skiller"/>
</subclass>
<subclass name="Sales" discriminator-value="2">
<property name="sell"/>
</subclass>
</class> </hibernate-mapping>
测试代码:
package com.dzq.test; import java.util.HashSet;
import java.util.Set; import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction; import com.dzq.domain.Department;
import com.dzq.domain.Employee;
import com.dzq.domain.Sales;
import com.dzq.domain.Skiller;
import com.dzq.utils.HibernateUntils; public class ManyToOne { public static void main(String[] args) {
add();
query(2);
}
public static void addEmAndDe(){
Employee em=new Employee();
Department depart=new Department();
depart.setName("FBI");
em.setDepart(depart);
em.setName("AK47");
HibernateUntils.add(depart);
HibernateUntils.add(em);
} public static void add(){
Session s=null;
Transaction ts=null;
try { Department dep=new Department();
dep.setName("FBI");
Employee e1=new Employee();
Skiller e2=new Skiller();
Sales e3=new Sales();
e1.setName("hi");
e1.setDepart(dep);
e2.setName("hello");
e2.setDepart(dep);
e2.setSkiller("skill");
e3.setName("fuck");
e3.setDepart(dep);
e3.setSell(100);
Set<Employee> empls=new HashSet<Employee>();
empls.add(e1);
empls.add(e2);
empls.add(e3);
dep.setEmpls(empls);
s=HibernateUntils.getSession();
ts=s.beginTransaction();
s.save(dep);
s.save(e1);
s.save(e2);
s.save(e3); ts.commit();
} catch (Exception e) {
ts.rollback();
throw new RuntimeException(e);
}finally{
if(s!=null){
s.close();
}
}
} public static Employee query(int id){
Session s=null;
try{
s=HibernateUntils.getSession();
Employee emp=(Employee) s.get(Employee.class, id);
//Hibernate.initialize(emp.getDepart());
System.out.println(emp.getClass());
return emp;
}finally{
if(s!=null){
s.close();
}
}
} }
表结构

2、每个子类一张表(joined-subclass) (表结构)

<class name="Employee" table="employee">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<joined-subclass name="Skiller" table="skiller">
<key column="employee_id"/>
<property name="skill"/>
</joined-subclass>
<joined-subclass name="Sales" table="sales">
<key column="employee_id"/>
<property name="sell"/>
</joined-subclass>
</class>

<class name="Employee" table="employee">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="type"/>
<property name="name"/>
<subclass name="Skiller">
<property name="net"/>
</subclass>
<subclass name=”Sales”">
<join table="sales">
<key column="employee_id"/>
<property name="sell"/>
</join>
</subclass>
</class>


<class name="Employee" abstract="true">
<id name="id">
<generator class="hilo"/>
</id>
<property name="name"/>
<union-subclass name="Skiller" table="skiller">
<property name="skill"/>
</union-subclass>
<union-subclass name="Sales" table="sales">
<property name="sell"/>
</union-subclass>
</class>
20160509-hibernate--继承映射的更多相关文章
- 【JavaEE】Hibernate继承映射,不用多态查询只查父表的方法
几个月前,我在博问里面发了一个问题:http://q.cnblogs.com/q/64900/,但是一直没有找到好的答案,关闭问题以后才自己解决了,在这里分享一下. 首先我重复一下场景,博问里面举的动 ...
- hibernate 继承映射关系( SINGLE_TABLE)
三种继承映射关系. 1,SINGLE_TABLE person student teacher 在一个表中,student和teacher继承自person,通过一个Discriminato ...
- Hibernate继承映射(@Inheritance)
继承映射在 Annotation 中使用 @Inheritance 注解,并且需要使用 strategy 属性指定继承策略,继承策略有 SINGLE_TABLE.TABLE_PER_CLASS 和 J ...
- SSH开发实践part3:hibernate继承映射
0 大家好.上次讲了关于hibernate中双向1-N的映射配置,可以参考:http://www.cnblogs.com/souvenir/p/3784510.html 实际项目中,对象间的关系比较复 ...
- Hibernate继承映射
在面向对象的程序领域中,类与类之间是有继承关系的,例如Java世界中只需要extends关键字就可以确定这两个类的父子关系,但是在关系数据库的世界中,表与表之间没有任何关键字可以明确指明这两张表的父子 ...
- web进修之—Hibernate 继承映射(5)
先看三个类的继承关系,Payment是父类,CashPayment和CreditCardPayment是Payment的子类: view plaincopy to clipboardprint p ...
- Hibernate 继承映射可能会遇到的错误
问题: 我们在配置hibernate的时候,默认是会配置下面的两个属性的 <property name="hibernate.default_catalog">hibe ...
- hibernate 继承映射关系( JOINED)
一个主表,其他的表每个都有自己的表来装填自己特有的部分,共同的部分就放在主表中. package com.bjsxt.hibernate; import javax.persistence.Ent ...
- hibernate 继承映射关系( TABLE_PER_CLASS)
Person,Student,Teacher各创建一个表,主键用一个中间表生成. package com.bjsxt.hibernate; import javax.persistence.Ent ...
- Hibernate 继承映射
@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn()public class Animal ...
随机推荐
- 【创建本地仓库】【for Centos】CentOS下创建本地repository
[日期]2014年4月24日 [平台]Centos 6.5 [工具]httpd yum-utils createrepo [步骤] 1)安装httpd. yum install httpd 2)安装y ...
- SRM 596 DIV 2
前段时间终于配置好了TopCoder的环境,所以就拿这场的DIV2练习了一下 1. 250pt FoxAndSightseeing 题意 给你n个城市的位置,他们在同一直线上,要求你跳过其中某一个城市 ...
- usb device selection
- 关于JavaScripting API您不知道的5件事
现在,许多 Java 开发人员都喜欢在 Java 平台中使用脚本语言,但是使用编译到 Java 字节码中的动态语言有时是不可行的.在某些情况中,直接编写一个 Java 应用程序的脚本 部分 或者在一个 ...
- 谷歌官方刷新组件SwipeRefreshLayout
今天开始使用谷歌的SwipeRefreshLayout,会记下一些坑 1.手动调用setRefreshing(true)不会出现刷新动画 原因是:SwipeRefreshLayout indicato ...
- A Tour of Go Interfaces
An interface type is defined by a set of methods. A value of interface type can hold any value that ...
- 备份数据表为insert 脚本
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- lazyload 图片延迟加载
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- zoj 1622 Switch 开关灯 简单枚举
ZOJ Problem Set - 1622 Switch Time Limit: 2 Seconds Memory Limit: 65536 KB There are N lights i ...
- 经验之巧妙的应用Map
后台: @RequestMapping("/cmci/v_divide_check_add.do") public String toDivideCheckAdd(HttpS ...