使用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(11NOT NULL default '0',
  `size` 
double default NULL,
  `name` 
varchar(50default NULL,
  `description` 
varchar(50default NULL,
  `width` 
double default NULL,
  `height` 
double default NULL,
  `
lendouble default NULL,
  
PRIMARY KEY  (`id`)
) ENGINE
=InnoDB DEFAULT CHARSET=gb2312;

CREATE TABLE `bottle` (
  `id` 
int(11NOT NULL default '0',
  `size` 
double default NULL,
  `name` 
varchar(20default NULL,
  `diameter` 
double default NULL,
  `height` 
double default NULL,
  `description` 
varchar(50default NULL,
  
PRIMARY KEY  (`id`)
) ENGINE
=InnoDB DEFAULT CHARSET=gb2312;

CREATE TABLE `hibernate_unique_key` (
  `next_hi` 
int(11NOT 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;
}

}


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;
}

}


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;
}


}


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>

 

 

<?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>

 

 

<?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>

 

测试代码:

 

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");

    }


}

原创粉丝点击