Hibernate继承映射--每个类一个数据表的实现案例
2007-03-14 13:10
615 查看
首先,我们有如下实体关系,Container,Box,Bottle,Container是Box和Bottle的父类
数据库结构如下:
其中hibernate_unique_key表为hibernate主键生成策略的hilo高低位算法使用
CREATE TABLE `box1` (
`id` varchar(50) NOT NULL default '0',
`size` double default NULL,
`name` varchar(50) default NULL,
`description` varchar(50) default NULL,
`width` double default NULL,
`height` double default NULL,
`len` double default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
CREATE TABLE `container1` (
`id` varchar(50) NOT NULL default '0',
`size` double default NULL,
`name` varchar(50) default NULL,
`description` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
CREATE TABLE `bottle1` (
`id` varchar(50) NOT NULL default '0',
`size` double default NULL,
`name` varchar(20) default NULL,
`diameter` double default NULL,
`height` double default NULL,
`description` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
CREATE TABLE `hibernate_unique_key` (
`next_hi` int(11) NOT NULL default '0',
PRIMARY KEY (`next_hi`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
实体定义如下:
package extendstestone;
//hibernate多态支持的策略,每个子类一张表
public abstract class Container ...{
private String containerId;
private double size;
private String name;
private String description;
public String getContainerId() ...{
return containerId;
}
public void setContainerId(String containerId) ...{
this.containerId = containerId;
}
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public double getSize() ...{
return size;
}
public void setSize(double size) ...{
this.size = size;
}
}
package extendstestone;
public class Box extends Container ...{
private double height;
private double length;
private double width;
public double getHeight() ...{
return height;
}
public void setHeight(double height) ...{
this.height = height;
}
public double getLength() ...{
return length;
}
public void setLength(double length) ...{
this.length = length;
}
public double getWidth() ...{
return width;
}
public void setWidth(double width) ...{
this.width = width;
}
}
package extendstestone;
public class Bottle extends Container ...{
private double diameter;
private double height;
public double getDiameter() ...{
return diameter;
}
public void setDiameter(double diameter) ...{
this.diameter = diameter;
}
public double getHeight() ...{
return height;
}
public void setHeight(double height) ...{
this.height = height;
}
}
配置文件:
<?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">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping package="extendstestone">
<class name="Container" table="container1">
<id name="containerId" column="id">
<generator class="uuid.hex"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<joined-subclass name="extendstestone.Box" table="box1">
<key column="id"></key>
<property name="height" column="height"></property>
<property name="width" column="width"></property>
<property name="length" column="len"></property>
</joined-subclass>
<joined-subclass name="extendstestone.Bottle" table="bottle1">
<key column="id"></key>
<property name="height" column="height"></property>
<property name="diameter" column="diameter"></property>
</joined-subclass>
</class>
</hibernate-mapping>
测试代码:
public static void main(String[] args) ...{
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction t=session.beginTransaction();
Box box1=new Box();
box1.setName("木箱子");
box1.setHeight(50);
box1.setLength(50);
box1.setWidth(50);
box1.setDescription("包装电视机");
box1.setSize(12500);
Bottle bottle1=new Bottle();
bottle1.setName("玻璃瓶子");
bottle1.setDescription("喝牛奶用");
bottle1.setSize(139.12);
bottle1.setDiameter(6);
bottle1.setHeight(12);
session.save(box1);
session.save(bottle1);
//取全部Box
Query query1=session.createQuery("from Box");
for(Iterator iter=query1.list().iterator();iter.hasNext();)...{
System.out.println(((Container)iter.next()).getName());
}
//取全部Bottle
Query query2=session.createQuery("from Bottle");
for(Iterator iter=query2.list().iterator();iter.hasNext();)...{
System.out.println(((Container)iter.next()).getName());
}
//取全部container
Query query3=session.createQuery("from Container");
for(Iterator iter=query3.list().iterator();iter.hasNext();)...{
System.out.println(((Container)iter.next()).getName());
}
t.commit();
System.out.println("success");
}
运行结果:
1
木箱子
2
玻璃瓶子
1
2
木箱子
玻璃瓶子
可以看到,直接对Box和Bottle的插入,会直接修改Container表,其主键是和对应的实体一致的
查询也可以根据from container, from bottle, from box分别就行查询
数据库结构如下:
其中hibernate_unique_key表为hibernate主键生成策略的hilo高低位算法使用
CREATE TABLE `box1` (
`id` varchar(50) NOT NULL default '0',
`size` double default NULL,
`name` varchar(50) default NULL,
`description` varchar(50) default NULL,
`width` double default NULL,
`height` double default NULL,
`len` double default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
CREATE TABLE `container1` (
`id` varchar(50) NOT NULL default '0',
`size` double default NULL,
`name` varchar(50) default NULL,
`description` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
CREATE TABLE `bottle1` (
`id` varchar(50) NOT NULL default '0',
`size` double default NULL,
`name` varchar(20) default NULL,
`diameter` double default NULL,
`height` double default NULL,
`description` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
CREATE TABLE `hibernate_unique_key` (
`next_hi` int(11) NOT NULL default '0',
PRIMARY KEY (`next_hi`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
实体定义如下:
package extendstestone;
//hibernate多态支持的策略,每个子类一张表
public abstract class Container ...{
private String containerId;
private double size;
private String name;
private String description;
public String getContainerId() ...{
return containerId;
}
public void setContainerId(String containerId) ...{
this.containerId = containerId;
}
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public double getSize() ...{
return size;
}
public void setSize(double size) ...{
this.size = size;
}
}
package extendstestone;
public class Box extends Container ...{
private double height;
private double length;
private double width;
public double getHeight() ...{
return height;
}
public void setHeight(double height) ...{
this.height = height;
}
public double getLength() ...{
return length;
}
public void setLength(double length) ...{
this.length = length;
}
public double getWidth() ...{
return width;
}
public void setWidth(double width) ...{
this.width = width;
}
}
package extendstestone;
public class Bottle extends Container ...{
private double diameter;
private double height;
public double getDiameter() ...{
return diameter;
}
public void setDiameter(double diameter) ...{
this.diameter = diameter;
}
public double getHeight() ...{
return height;
}
public void setHeight(double height) ...{
this.height = height;
}
}
配置文件:
<?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">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping package="extendstestone">
<class name="Container" table="container1">
<id name="containerId" column="id">
<generator class="uuid.hex"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<joined-subclass name="extendstestone.Box" table="box1">
<key column="id"></key>
<property name="height" column="height"></property>
<property name="width" column="width"></property>
<property name="length" column="len"></property>
</joined-subclass>
<joined-subclass name="extendstestone.Bottle" table="bottle1">
<key column="id"></key>
<property name="height" column="height"></property>
<property name="diameter" column="diameter"></property>
</joined-subclass>
</class>
</hibernate-mapping>
测试代码:
public static void main(String[] args) ...{
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction t=session.beginTransaction();
Box box1=new Box();
box1.setName("木箱子");
box1.setHeight(50);
box1.setLength(50);
box1.setWidth(50);
box1.setDescription("包装电视机");
box1.setSize(12500);
Bottle bottle1=new Bottle();
bottle1.setName("玻璃瓶子");
bottle1.setDescription("喝牛奶用");
bottle1.setSize(139.12);
bottle1.setDiameter(6);
bottle1.setHeight(12);
session.save(box1);
session.save(bottle1);
//取全部Box
Query query1=session.createQuery("from Box");
for(Iterator iter=query1.list().iterator();iter.hasNext();)...{
System.out.println(((Container)iter.next()).getName());
}
//取全部Bottle
Query query2=session.createQuery("from Bottle");
for(Iterator iter=query2.list().iterator();iter.hasNext();)...{
System.out.println(((Container)iter.next()).getName());
}
//取全部container
Query query3=session.createQuery("from Container");
for(Iterator iter=query3.list().iterator();iter.hasNext();)...{
System.out.println(((Container)iter.next()).getName());
}
t.commit();
System.out.println("success");
}
运行结果:
1
木箱子
2
玻璃瓶子
1
2
木箱子
玻璃瓶子
可以看到,直接对Box和Bottle的插入,会直接修改Container表,其主键是和对应的实体一致的
查询也可以根据from container, from bottle, from box分别就行查询
相关文章推荐
- Hibernate继承映射--每个子类一个数据表的实现案例
- Hibernate 继承关系映射——每个类一个数据表
- Hibernate 继承映射实现方式之每个具体类对应一张表
- Hibernate如何实现数据表映射的继承关系
- J2EE系列之Hibernate4学习笔记(十二)--Hibernate继承关系映射(每个类对应一个表)
- Hibernate 中继承映射之二 每个具体类一个表
- Hibernate映射类继承之每个类层次结构一张表(父子类共用一张表,一个映射文件)
- Hibernate映射类继承之每个带有联合的具体类一张表(每个子类各一张表,共用一个父类映射文件)
- J2EE系列之Hibernate4学习笔记(十)--Hibernate继承关系映射(每个具体类对应一个表)
- 用hibernate实现java的类继承-每个类一个表
- 同一个数据库实例,不同用户下多表创建视图,Hibernate完成ORM映射,Spring整合,后台实现
- Hibernate映射类继承之每个类一张表(父子类都有各自的表,并共用一个映射文件)
- hibernate笔记--继承映射关系的三种实现方式
- hibernate继承映射[映射单独一个tablexml版本]实例【与19相同,方式不同,未验证】(二十)
- hibernate继承映射3种实现形式归纳
- Hibernate继承映射:每个子类一张表(joined-subclass)表结构
- 一个使用C#语言实现的数据比较案例
- 在Hibernate中实现复杂的数据映射
- hibernate继承映射之每个子类一张表(无辨识)
- hibernate继承映射之每个具体类一张表