說明:利用category欄位來區分商品類別,如此一來不管是Book或是DVD皆可以存放在此Table中。Table per class hierarchy:表與類別的一對多關係。1.建立t_item表格:drop table if exists t_item;create table t_item(id int(11) auto_increment,category varchar(10) not null default '',name varchar(50) default '',manufacture varchar(50) default '',regioncode varchar(30) default '',pagecount int(11),primary key(id));2.建立TItem.java及TItem.hbm.xml,其內容分別如下:package org.redsaga.quickstart;// Generated 2009/8/6 下午 11:08:15 by Hibernate Tools 3.2.4.GA/*** TItem generated by hbm2java*/public class TItem implements java.io.Serializable {private Integer id;private String category;private String name;private String manufacture;public TItem() {}public TItem(String category) {this.category = category;}public TItem(String category, String name, String manufacture) {this.category = category;this.name = name;this.manufacture = manufacture;}public Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public String getCategory() {return this.category;}public void setCategory(String category) {this.category = category;}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;}}==================================<?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/6 ?U?? 11:08:15 by Hibernate Tools 3.2.4.GA --><hibernate-mapping><class name="org.redsaga.quickstart.TItem" table="t_item" catalog="manning"><id name="id" type="java.lang.Integer"><column name="id" /><generator class="identity" /></id><discriminator column="category" length="10" type="string" not-null="true"/><property name="name" type="string"><column name="name" length="50" /></property><property name="manufacture" type="string"><column name="manufacture" length="50" /></property><subclass name="org.redsaga.quickstart.TDVD" discriminator-value="2"><property name="regioncode" type="string"><column name="regioncode" length="30" /></property></subclass><subclass name="org.redsaga.quickstart.TBook" discriminator-value="1"><property name="pagecount" type="java.lang.Integer"><column name="pagecount" /></property></subclass></class></hibernate-mapping>=====================================3.建立TBook.java及TDVD.java,其內容分別如下:package org.redsaga.quickstart;public class TBook extends TItem implements java.io.Serializable {private Integer pagecount;public TBook(){}public Integer getPagecount() {return this.pagecount;}public void setPagecount(Integer pagecount) {this.pagecount = pagecount;}}====================================package org.redsaga.quickstart;public class TDVD extends TItem implements java.io.Serializable {private String regioncode;public TDVD(){}public String getRegioncode() {return this.regioncode;}public void setRegioncode(String regioncode) {this.regioncode = regioncode;}}======================================4.修改Hibnerate.cfg.xml:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property><property name="hibernate.connection.password">action</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/manning</property><property name="hibernate.connection.username">manning</property><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- 將SQL輸出到Console --><property name="hibernate.show_sql">true</property><!-- 使用JDBC Transaction --><property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property><!-- 將TUser.hbm.xml加到Hibernate.cfg.xml檔案中 --><mapping resource="org/redsaga/quickstart/TUser.hbm.xml"/><!-- 將TItem.hbm.xml加到Hibernate.cfg.xml檔案中 --><mapping resource="org/redsaga/quickstart/TItem.hbm.xml"/></session-factory></hibernate-configuration>=====================================5.建立JUnit測試Method(testItem),內容如下:/*** testItem Table per class hierarchy練習* @throws Exception*/public void testItem() throws Exception {Transaction tran = null;TBook book = null;TDVD dvd = null;List list = null;Iterator it = null;try{tran = session.beginTransaction();//新增書籍資料book = new TBook();book.setCategory("1");book.setName("倚天屠龍記");book.setManufacture("金庸");book.setPagecount(288);session.save(book);session.flush();tran.commit();tran = session.beginTransaction();//新增DVD資料dvd = new TDVD();dvd.setCategory("2");dvd.setName("今天不回家");dvd.setManufacture("全家");dvd.setRegioncode("亞洲");session.save(dvd);session.flush();tran.commit();//查詢 TBook資料list = session.createQuery(" from TBook").list();it = list.iterator();while(it.hasNext()){book = (TBook)it.next();System.out.println("Book:"+book.getName()+","+book.getManufacture()+","+book.getPagecount());}//查詢 TDVD資料list = session.createQuery(" from TDVD").list();it = list.iterator();while(it.hasNext()){dvd = (TDVD)it.next();System.out.println("DVD:"+dvd.getName()+","+dvd.getManufacture()+","+dvd.getRegioncode());}}catch(HibernateException e){e.printStackTrace();}}==================================6.測試結果:console輸出結果:Hibernate: insert into manning.t_item (name, manufacture, pagecount, category) values (?, ?, ?, '1')Hibernate: insert into manning.t_item (name, manufacture, regioncode, category) values (?, ?, ?, '2')Hibernate: select tbook0_.id as id1_, tbook0_.name as name1_, tbook0_.manufacture as manufact4_1_, tbook0_.pagecount as pagecount1_ from manning.t_item tbook0_ where tbook0_.category='1'Book:倚天屠龍記,金庸,288Hibernate: select tdvd0_.id as id1_, tdvd0_.name as name1_, tdvd0_.manufacture as manufact4_1_, tdvd0_.regioncode as regioncode1_ from manning.t_item tdvd0_ where tdvd0_.category='2'DVD:今天不回家,全家,亞洲
2009年8月7日 星期五
Hibernate實作Table per class hierarchy例子
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言