使用Subselect标签构造持久类查询
来源:互联网 发布:21天学通java怎么样 编辑:程序博客网 时间:2024/06/06 20:48
前面文章阐述了,如果在Container,Box,Bottle三者的继承关系中配置HBM文件(box,bottle继承自Container)
现在,有一种更加简便的方式,使数据库中不需要建立一个box。bottle共享的表,而是分别建立Box和Bottle的表,这样更加复合面向对象的思想,而查询container则通过HBM配置文件来进行
Container持久类 其中Xparam,Yparam,Zparam对应box,bottle中的length等属性,只要保证都能包括Box,Bottle中的属性就可以,也就是说container属性是Box和Bottle和合集
本例子使用了hilo主键生成器策略所以,需要在数据库中建立一张维护高位值的表,并给出一个高位数值
CREATE TABLE `box` (
`id` int(11) 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 `bottle` (
`id` int(11) 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;
`id` int(11) 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 `bottle` (
`id` int(11) 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 subselect;
public class Container ...{
private String id;
private double size;
private String name;
private String description;
private double xparam;
private double yparam;
private double zparam;
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
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;
}
public double getXparam() ...{
return xparam;
}
public void setXparam(double xparam) ...{
this.xparam = xparam;
}
public double getYparam() ...{
return yparam;
}
public void setYparam(double yparam) ...{
this.yparam = yparam;
}
public double getZparam() ...{
return zparam;
}
public void setZparam(double zparam) ...{
this.zparam = zparam;
}
}
public class Container ...{
private String id;
private double size;
private String name;
private String description;
private double xparam;
private double yparam;
private double zparam;
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
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;
}
public double getXparam() ...{
return xparam;
}
public void setXparam(double xparam) ...{
this.xparam = xparam;
}
public double getYparam() ...{
return yparam;
}
public void setYparam(double yparam) ...{
this.yparam = yparam;
}
public double getZparam() ...{
return zparam;
}
public void setZparam(double zparam) ...{
this.zparam = zparam;
}
}
Box持久类
package subselect;
public class Box ...{
private int id;
private double size;
private String name;
private String description;
private double width;
private double length;
private double height;
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
public double getHeight() ...{
return height;
}
public void setHeight(double height) ...{
this.height = height;
}
public int getId() ...{
return id;
}
public void setId(int id) ...{
this.id = id;
}
public double getLength() ...{
return length;
}
public void setLength(double length) ...{
this.length = length;
}
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;
}
public double getWidth() ...{
return width;
}
public void setWidth(double width) ...{
this.width = width;
}
}
public class Box ...{
private int id;
private double size;
private String name;
private String description;
private double width;
private double length;
private double height;
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
public double getHeight() ...{
return height;
}
public void setHeight(double height) ...{
this.height = height;
}
public int getId() ...{
return id;
}
public void setId(int id) ...{
this.id = id;
}
public double getLength() ...{
return length;
}
public void setLength(double length) ...{
this.length = length;
}
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;
}
public double getWidth() ...{
return width;
}
public void setWidth(double width) ...{
this.width = width;
}
}
Bottle持久类
package subselect;
public class Bottle ...{
private int id;
private double size;
private String name;
private String description;
private double diameter;
private double height;
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
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;
}
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 double getSize() ...{
return size;
}
public void setSize(double size) ...{
this.size = size;
}
}
public class Bottle ...{
private int id;
private double size;
private String name;
private String description;
private double diameter;
private double height;
public String getDescription() ...{
return description;
}
public void setDescription(String description) ...{
this.description = description;
}
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;
}
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 double getSize() ...{
return size;
}
public void setSize(double size) ...{
this.size = size;
}
}
Mapping文件:
<?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="subselect">
<!-- mutable指定Container的只读属性,只能从select中生成 -->
<class name="Container" mutable="false">
<subselect>
select id,size,name,description,diameter as xparam,diameter as yparam,height as zparam from Bottle
union
select id,size,name,description,width as xparam,len as yparam,height as zparam from Box
</subselect>
<synchronize table="Box"/>
<synchronize table="Bottle"/>
<id name="id" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<property name="xparam" column="xparam"></property>
<property name="yparam" column="yparam"></property>
<property name="zparam" column="zparam"></property>
</class>
</hibernate-mapping>
<!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="subselect">
<!-- mutable指定Container的只读属性,只能从select中生成 -->
<class name="Container" mutable="false">
<subselect>
select id,size,name,description,diameter as xparam,diameter as yparam,height as zparam from Bottle
union
select id,size,name,description,width as xparam,len as yparam,height as zparam from Box
</subselect>
<synchronize table="Box"/>
<synchronize table="Bottle"/>
<id name="id" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<property name="xparam" column="xparam"></property>
<property name="yparam" column="yparam"></property>
<property name="zparam" column="zparam"></property>
</class>
</hibernate-mapping>
<?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="subselect">
<class name="Box" table="box">
<id name="id">
<column name="id"></column>
<generator class="hilo"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<property name="width" column="width"></property>
<property name="height" column="height"></property>
<property name="length" column="len"></property>
</class>
</hibernate-mapping>
<!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="subselect">
<class name="Box" table="box">
<id name="id">
<column name="id"></column>
<generator class="hilo"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<property name="width" column="width"></property>
<property name="height" column="height"></property>
<property name="length" column="len"></property>
</class>
</hibernate-mapping>
<?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="subselect">
<class name="Bottle" table="bottle">
<id name="id">
<column name="id"></column>
<generator class="hilo"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<property name="diameter" column="diameter"></property>
<property name="height" column="height"></property>
</class>
</hibernate-mapping>
<!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="subselect">
<class name="Bottle" table="bottle">
<id name="id">
<column name="id"></column>
<generator class="hilo"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<property name="diameter" column="diameter"></property>
<property name="height" column="height"></property>
</class>
</hibernate-mapping>
测试代码:
package subselect;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class test ...{
/** *//**
* @param args
*/
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 box=new Box();
box.setName("纸箱子");
box.setSize(100);
box.setDescription("装电视机");
box.setLength(100);
box.setWidth(200);
Bottle bottle=new Bottle();
bottle.setName("空瓶子");
bottle.setSize(100);
bottle.setHeight(100);
bottle.setDiameter(100);
bottle.setDescription("喝牛奶用");
session.save(box);
session.save(bottle);
t.commit();
Query query=session.createQuery("from Container");
List a=query.list();
for(int i=0;i<a.size();i++)...{
Container c=(Container)a.get(i);
System.out.println(c.getId()+"-"+c.getName());
}
System.out.println("success");
}
}
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class test ...{
/** *//**
* @param args
*/
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 box=new Box();
box.setName("纸箱子");
box.setSize(100);
box.setDescription("装电视机");
box.setLength(100);
box.setWidth(200);
Bottle bottle=new Bottle();
bottle.setName("空瓶子");
bottle.setSize(100);
bottle.setHeight(100);
bottle.setDiameter(100);
bottle.setDescription("喝牛奶用");
session.save(box);
session.save(bottle);
t.commit();
Query query=session.createQuery("from Container");
List a=query.list();
for(int i=0;i<a.size();i++)...{
Container c=(Container)a.get(i);
System.out.println(c.getId()+"-"+c.getName());
}
System.out.println("success");
}
}
- 使用Subselect标签构造持久类查询
- Hibernate中子查询(subselect)的使用
- 数据检索-子查询(subselect)
- @Subselect映射,子查询,视图.
- MyBatis中使用bind标签构造模糊查询失败的解决方法
- 使用HQL的select查询字段并构造出类
- 使用Ajax+JQuery构造分页查询列表
- hibernate 通过子查询预抓取集合 fetch subselect join (最后遗留疑问)
- Web_PHP_DedeCMS_{dede:php}标签嵌套查询使用;
- 数据库查询比较标签的使用
- JPA学习(五):java持久化查询语言JPQL--动态查询的使用及语法限制
- 在JSP中使用标签构造防盗链
- 使用映射文件定制持久化类
- Hibernate使用xml配置持久化类
- Hibernate使用注解配置持久化类
- 【JPQL】—查询参数、命名查询和使用构造器查询
- Where vs. Subselect/Subcube
- Ruby on Rails,使用where方法对持久化对象进行条件查询
- 从ACCESS转至SQLSERVER
- JAVA中的日期类型与字符类型的转换
- RPG简单例子
- 使用联结语法代替not运算的小技巧
- AJAX
- 使用Subselect标签构造持久类查询
- p2p直播系统
- Struts的文件上传
- 屏蔽鼠标右健的javascipt
- Java String 对象剖析
- pl/sql 笔记1
- h264视频编解码
- Asp.net 2.0 Treeview 无限级无刷新示例
- 棋牌软件