您的位置:首页 > 其它

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分别就行查询
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐