Hibernate继承映射--每个子类一个数据表的实现案例
2007-03-14 13:30
615 查看
这种方式,不再针对父类Container有专门的数据表和映射文件,hibernate隐性的通过
Container与两个子类的继承关系实现这一效果
数据库结构:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `box2` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL default '0',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`size` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`name` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`description` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`width` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`height` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`len` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PRIMARY KEY (`id`)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `bottle2` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL default '0',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`size` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`name` varchar(20) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`diameter` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`height` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`description` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PRIMARY KEY (`id`)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
实体关系:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package extendstesttwo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Bottle extends Container ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double diameter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getDiameter() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return diameter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setDiameter(double diameter) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.diameter = diameter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getHeight() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.height = height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package extendstesttwo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Box extends Container ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getHeight() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.height = height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getLength() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setLength(double length) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.length = length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getWidth() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setWidth(double width) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.width = width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package extendstesttwo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//hibernate多态支持的策略,每个子类一张表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public abstract class Container ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String containerId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double size;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String description;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getContainerId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return containerId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setContainerId(String containerId) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.containerId = containerId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getDescription() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return description;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setDescription(String description) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.description = description;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setName(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getSize() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return size;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setSize(double size) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.size = size;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
映射文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="extendstesttwo">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Bottle" table="bottle2">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="containerId" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="size" column="size"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="name" column="name"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="description" column="description"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="height" column="height"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="diameter" column="diameter"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="extendstesttwo">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Box" table="box2">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="containerId" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="size" column="size"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="name" column="name"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="description" column="description"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="height" column="height"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="width" column="width"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="length" column="len"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
测试代码:
需要注意的是,由于没有Container的映射关系,此处需要用完成的包名
"from extendstesttwo.Container"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Configuration cfg=new Configuration();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cfg.configure();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SessionFactory sf=cfg.buildSessionFactory();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session=sf.openSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Transaction t=session.beginTransaction();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Box box1=new Box();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setName("木箱子");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setHeight(50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setLength(50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setWidth(50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setDescription("包装电视机");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setSize(12500);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Bottle bottle1=new Bottle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setName("玻璃瓶子");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setDescription("喝牛奶用");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setSize(139.12);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setDiameter(6);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setHeight(12);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(box1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(bottle1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取全部Box
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query1=session.createQuery("from Box");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(Iterator iter=query1.list().iterator();iter.hasNext();)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(((Container)iter.next()).getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取全部Bottle
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query2=session.createQuery("from Bottle");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(Iterator iter=query2.list().iterator();iter.hasNext();)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(((Container)iter.next()).getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取全部container
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query3=session.createQuery("from extendstesttwo.Container");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(Iterator iter=query3.list().iterator();iter.hasNext();)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(((Container)iter.next()).getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
t.commit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果:
木箱子
Hibernate: select bottle0_.id as id0_, bottle0_.size as size0_, bottle0_.name as name0_, bottle0_.description as descript4_0_, bottle0_.height as height0_, bottle0_.diameter as diameter0_ from bottle2 bottle0_
玻璃瓶子
Hibernate: select box0_.id as id1_, box0_.size as size1_, box0_.name as name1_, box0_.description as descript4_1_, box0_.height as height1_, box0_.width as width1_, box0_.len as len1_ from box2 box0_
Hibernate: select bottle0_.id as id0_, bottle0_.size as size0_, bottle0_.name as name0_, bottle0_.description as descript4_0_, bottle0_.height as height0_, bottle0_.diameter as diameter0_ from bottle2 bottle0_
木箱子
玻璃瓶子
success
可以看到,这样也可以实现继承的查询
总结:目前继承查询有了三种方法了:<subselect>标签加上这两种继承(每个子类一个表,每个类一个表)策略(三种方式在我的blog的经验技巧之hibernate中都有相关文章,本文是第二种策略)
Container与两个子类的继承关系实现这一效果
数据库结构:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `box2` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL default '0',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`size` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`name` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`description` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`width` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`height` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`len` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PRIMARY KEY (`id`)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `bottle2` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL default '0',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`size` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`name` varchar(20) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`diameter` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`height` double default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`description` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PRIMARY KEY (`id`)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
实体关系:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package extendstesttwo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Bottle extends Container ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double diameter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getDiameter() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return diameter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setDiameter(double diameter) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.diameter = diameter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getHeight() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.height = height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package extendstesttwo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Box extends Container ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getHeight() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.height = height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getLength() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setLength(double length) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.length = length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getWidth() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setWidth(double width) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.width = width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package extendstesttwo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//hibernate多态支持的策略,每个子类一张表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public abstract class Container ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String containerId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double size;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String description;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getContainerId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return containerId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setContainerId(String containerId) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.containerId = containerId;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getDescription() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return description;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setDescription(String description) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.description = description;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setName(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getSize() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return size;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setSize(double size) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.size = size;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
映射文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="extendstesttwo">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Bottle" table="bottle2">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="containerId" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="size" column="size"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="name" column="name"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="description" column="description"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="height" column="height"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="diameter" column="diameter"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="extendstesttwo">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Box" table="box2">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="containerId" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="size" column="size"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="name" column="name"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="description" column="description"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="height" column="height"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="width" column="width"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="length" column="len"></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
测试代码:
需要注意的是,由于没有Container的映射关系,此处需要用完成的包名
"from extendstesttwo.Container"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Configuration cfg=new Configuration();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cfg.configure();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SessionFactory sf=cfg.buildSessionFactory();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session=sf.openSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Transaction t=session.beginTransaction();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Box box1=new Box();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setName("木箱子");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setHeight(50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setLength(50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setWidth(50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setDescription("包装电视机");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
box1.setSize(12500);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Bottle bottle1=new Bottle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setName("玻璃瓶子");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setDescription("喝牛奶用");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setSize(139.12);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setDiameter(6);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bottle1.setHeight(12);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(box1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(bottle1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取全部Box
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query1=session.createQuery("from Box");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(Iterator iter=query1.list().iterator();iter.hasNext();)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(((Container)iter.next()).getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取全部Bottle
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query2=session.createQuery("from Bottle");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(Iterator iter=query2.list().iterator();iter.hasNext();)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(((Container)iter.next()).getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取全部container
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query3=session.createQuery("from extendstesttwo.Container");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(Iterator iter=query3.list().iterator();iter.hasNext();)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(((Container)iter.next()).getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
t.commit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果:
木箱子
Hibernate: select bottle0_.id as id0_, bottle0_.size as size0_, bottle0_.name as name0_, bottle0_.description as descript4_0_, bottle0_.height as height0_, bottle0_.diameter as diameter0_ from bottle2 bottle0_
玻璃瓶子
Hibernate: select box0_.id as id1_, box0_.size as size1_, box0_.name as name1_, box0_.description as descript4_1_, box0_.height as height1_, box0_.width as width1_, box0_.len as len1_ from box2 box0_
Hibernate: select bottle0_.id as id0_, bottle0_.size as size0_, bottle0_.name as name0_, bottle0_.description as descript4_0_, bottle0_.height as height0_, bottle0_.diameter as diameter0_ from bottle2 bottle0_
木箱子
玻璃瓶子
success
可以看到,这样也可以实现继承的查询
总结:目前继承查询有了三种方法了:<subselect>标签加上这两种继承(每个子类一个表,每个类一个表)策略(三种方式在我的blog的经验技巧之hibernate中都有相关文章,本文是第二种策略)
相关文章推荐
- Hibernate继承映射--每个类一个数据表的实现案例
- Hibernate映射类继承之每个带有联合的具体类一张表(每个子类各一张表,共用一个父类映射文件)
- Hibernate 继承关系映射——每个类一个数据表
- J2EE系列之Hibernate4学习笔记(十二)--Hibernate继承关系映射(每个类对应一个表)
- Hibernate/JPA中的继承映射 实体extends的关系(每个子类独立一个表)
- Hibernate 中继承映射之二 每个具体类一个表
- Hibernate 继承映射实现方式之每个具体类对应一张表
- Hibernate学习笔记:继承映射之每个子类一张表,使用辨别标志
- Hibernate继承映射:每个子类一张表(joined-subclass)表结构
- hibernate继承映射策略之每个子类一张表
- hibernate继承关系映射关系方法(二)--每个子类一张表
- hibernate继承映射之每个子类一张表(无辨识)
- Hibernate学习笔记:继承映射之每个子类一张表
- J2EE系列之Hibernate4学习笔记(十)--Hibernate继承关系映射(每个具体类对应一个表)
- hibernate继承映射关系 —— 每个子类映射成一张表
- 用hibernate实现java的类继承-每个类一个表
- 010——hibernate具体表继承:每个子类一个表
- Hibernate继承映射方式之每个子类一张表
- Hibernate如何实现数据表映射的继承关系
- Hibernate继承映射:每个子类一张表(joined-subclass)表结构