testPerSubClass Table per subclass :每個子類別對應一張表,並與主類別共用主表1.建立表格:
drop table if exists t_dvd02;drop table if exists t_book02;drop table if exists t_item02;create table t_item02(id int(11) auto_increment,name varchar(30) default '',manufacture varchar(50) default '',primary key(id));create table t_book02(id int(11),pagecount int(4),primary key(id),foreign key(id) references t_item02(id));create table t_dvd02(id int(11),regioncode varchar(30),primary key(id),foreign key(id) references t_item02(id));2.建立TItem02.hbm.xml及TBook02.hbm.xml及TDvd02.hbm.xml:<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2009/8/7 ?W?? 11:20:42 by Hibernate Tools 3.2.4.GA --><hibernate-mapping><class name="org.redsaga.quickstart.TItem02" table="t_item02" catalog="manning"><id name="id" type="java.lang.Integer"><column name="id" /><generator class="identity" /></id><property name="name" type="string"><column name="name" length="30" /></property><property name="manufacture" type="string"><column name="manufacture" length="50" /></property><joined-subclass name="org.redsaga.quickstart.TDvd02" table="t_dvd02"><key column="id"/><property name="regioncode" column="regioncode" type="string"/></joined-subclass><joined-subclass name="org.redsaga.quickstart.TBook02" table="t_book02"><key column="id"/><property name="pagecount" column="pagecount" type="java.lang.Integer"></property></joined-subclass></class></hibernate-mapping>====================================<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2009/8/7 ?W?? 11:20:42 by Hibernate Tools 3.2.4.GA --><hibernate-mapping><class name="org.redsaga.quickstart.TBook02" table="t_book02" catalog="manning"><id name="id" type="int"><column name="id" /><generator class="foreign"><param name="property">TItem02</param></generator></id><one-to-one name="TItem02" class="org.redsaga.quickstart.TItem02" constrained="true"></one-to-one><property name="pagecount" type="java.lang.Integer"><column name="pagecount" /></property></class></hibernate-mapping>======================================<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2009/8/7 ?W?? 11:20:42 by Hibernate Tools 3.2.4.GA --><hibernate-mapping><class name="org.redsaga.quickstart.TDvd02" table="t_dvd02" catalog="manning"><id name="id" type="int"><column name="id" /><generator class="foreign"><param name="property">TItem02</param></generator></id><one-to-one name="TItem02" class="org.redsaga.quickstart.TItem02" constrained="true"></one-to-one><property name="regioncode" type="string"><column name="regioncode" length="30" /></property></class></hibernate-mapping>======================================3.建立TItem02.java及TBook02.java及TDvd02.java:package org.redsaga.quickstart;// Generated 2009/8/7 上午 11:20:42 by Hibernate Tools 3.2.4.GA/*** TItem02 generated by hbm2java*/public class TItem02 implements java.io.Serializable {private Integer id;private String name;private String manufacture;public TItem02() {}public TItem02(String name, String manufacture) {this.name = name;this.manufacture = manufacture;}public Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public String getManufacture() {return this.manufacture;}public void setManufacture(String manufacture) {this.manufacture = manufacture;}}===================================package org.redsaga.quickstart;// Generated 2009/8/7 上午 11:20:42 by Hibernate Tools 3.2.4.GA/*** TBook02 generated by hbm2java*/public class TBook02 extends TItem02 implements java.io.Serializable {private Integer pagecount;public TBook02() {}public TBook02(Integer pagecount) {this.pagecount = pagecount;}public Integer getPagecount() {return this.pagecount;}public void setPagecount(Integer pagecount) {this.pagecount = pagecount;}}===================================package org.redsaga.quickstart;// Generated 2009/8/7 上午 11:20:42 by Hibernate Tools 3.2.4.GA/*** TDvd02 generated by hbm2java*/public class TDvd02 extends TItem02 implements java.io.Serializable {private int id;private String regioncode;public TDvd02() {}public TDvd02(String regioncode) {this.regioncode = regioncode;}public String getRegioncode() {return this.regioncode;}public void setRegioncode(String regioncode) {this.regioncode = regioncode;}}====================================4.建立JUnit測試Method(testPerSubClass):/*** testPerSubClass Table per subclass 每個子類別對應一張表,並與主類別共用主表* @throws Exception*/public void testPerSubClass() throws Exception {Transaction tran = null;TBook02 book = new TBook02();TDvd02 dvd = new TDvd02();book.setName("倚天屠龍記");book.setManufacture("金庸");book.setPagecount(288);dvd.setName("今天不回家");dvd.setManufacture("全家");dvd.setRegioncode("亞洲");try{tran = session.beginTransaction();session.save(book);session.save(dvd);session.flush();tran.commit();List list = session.createQuery(" from TItem02").list();Iterator it = list.iterator();while(it.hasNext()){TItem02 item = (TItem02)it.next();System.out.println(item.getId()+","+item.getName()+","+item.getManufacture());}}catch(HibernateException e){e.printStackTrace();}}==================================5.測試結果:console結果:
Hibernate: insert into manning.t_item02 (name, manufacture) values (?, ?)Hibernate: insert into t_book02 (pagecount, id) values (?, ?)Hibernate: insert into manning.t_item02 (name, manufacture) values (?, ?)Hibernate: insert into t_dvd02 (regioncode, id) values (?, ?)Hibernate: select titem02x0_.id as id5_, titem02x0_.name as name5_, titem02x0_.manufacture as manufact3_5_, titem02x0_1_.regioncode as regioncode6_, titem02x0_2_.pagecount as pagecount7_, case when titem02x0_1_.id is not null then 1 when titem02x0_2_.id is not null then 2 when titem02x0_.id is not null then 0 end as clazz_ from manning.t_item02 titem02x0_ left outer join t_dvd02 titem02x0_1_ on titem02x0_.id=titem02x0_1_.id left outer join t_book02 titem02x0_2_ on titem02x0_.id=titem02x0_2_.id1,倚天屠龍記,金庸2,今天不回家,全家
2009年8月7日 星期五
Hibernate實作Table per subclass之例子
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言