Hibernate實體層次設(shè)計(四)

          Table per class hierarchy

          前兩種方式,都要進行多表操作,這樣帶來的性能的消耗量是相當可觀的,不利于高并發(fā)量的數(shù)據(jù)存取。
          Table per class hierarchy提供了另外一中選擇。
          實際開發(fā)中,通過冗余字段表達同類型數(shù)據(jù)是很多情況下的選擇。
          建立一個數(shù)據(jù)表T_Item包含了所有商品可能用到的字段。另外,為了區(qū)分不同的商品,還需要引入一個用于區(qū)分的字段。

          1.數(shù)據(jù)表如下圖:


          2.數(shù)據(jù)表定義sql
          use sample;
          DROP TABLE T_Item;

          CREATE TABLE T_Item (
                 id 
          INT NOT NULL AUTO_INCREMENT
               , category 
          VARCHAR(10)
               , name 
          VARCHAR(50)
               , manufacturer 
          VARCHAR(50)
               , regioncode 
          VARCHAR(30)
               , pagecount 
          INT
               , 
          PRIMARY KEY (id)
          );


          3.配置文件

          TItem.hbm.xml:
          注意,discriminator定義要緊跟在id定義之后。如果在property之后定義,會報錯誤。

          (meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)

          <?xml version="1.0"?>
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          >
          <hibernate-mapping package="cn.blogjava.start">

              
          <class name="TItem" table="T_Item" catalog="sample">
                  
          <id name="id" column="id">
                      
          <generator class="native" />
                  
          </id>
                  
                  
          <discriminator column="category" type="string"></discriminator>
                          
                  
          <property name="name" column="name"/>
                  
          <property name="manufacturer" column="manufacturer"/>        
                  
                  
          <subclass name="TBook" discriminator-value="1">
                      
          <property name="pageCount" column="pagecount"></property>
                  
          </subclass>
                  
                  
          <subclass name="TDVD" discriminator-value="2">
                      
          <property name="regionCode" column="regioncode"></property>
                  
          </subclass>        
                         
              
          </class>
          </hibernate-mapping>

          4.測試代碼
          package cn.blogjava.start;

          import java.util.Iterator;
          import java.util.List;

          import junit.framework.Assert;
          import junit.framework.TestCase;

          import org.hibernate.HibernateException;
          import org.hibernate.Session;
          import org.hibernate.SessionFactory;
          import org.hibernate.Transaction;
          import org.hibernate.cfg.Configuration;


          public class HibernateTest extends TestCase {
              
              Session session 
          = null;

              
          protected void setUp() {
                  
          try {

                      Configuration config 
          = new Configuration().configure();
                      SessionFactory sessionFactory 
          = config.buildSessionFactory();
                      session 
          = sessionFactory.openSession();
                      
                  } 
          catch (HibernateException e) {
                      e.printStackTrace();
                  }        
              }

              
          protected void tearDown() {
                  
          try {
                      session.close();        
                  } 
          catch (HibernateException e) {
                      e.printStackTrace();
                  }        
              }    
              
              
          public void testInsert() {
                  Transaction tran 
          = null;
                  
          try {
                      tran 
          = session.beginTransaction();
                      TBook book1 
          = new TBook();
                      
                      book1.setManufacturer(
          "byf");
                      book1.setName(
          "Java beginner");
                      book1.setPageCount(
          501);
                      
                      TBook book2 
          = new TBook();
                      book2.setManufacturer(
          "yyy");
                      book2.setName(
          "Thinking in java");
                      book2.setPageCount(
          1201);
                      
                      TDVD dvd1 
          = new TDVD();
                      dvd1.setManufacturer(
          "columnibia");
                      dvd1.setName(
          "Lord king");
                      dvd1.setRegionCode(
          "5 area");
                      
                      TDVD dvd2 
          = new TDVD();
                      dvd2.setManufacturer(
          "sony");
                      dvd2.setName(
          "Forrest gump");
                      dvd2.setRegionCode(
          "3 area");            

                      session.save(book1);
                      session.save(book2);
                      session.save(dvd1);
                      session.save(dvd2);
                      session.flush();
                      tran.commit();
                      Assert.assertEquals(book1.getId().intValue()
          >0true);
                      Assert.assertEquals(book2.getId().intValue()
          >0true);
                      Assert.assertEquals(dvd1.getId().intValue()
          >0true);
                      Assert.assertEquals(dvd2.getId().intValue()
          >0true);
                  } 
          catch (HibernateException e) {
                      
          // TODO: handle exception
                      e.printStackTrace();
                      Assert.fail(e.getMessage());
                      
          if(tran != null) {
                          
          try {
                              tran.rollback();
                          } 
          catch (Exception e1) {
                              
          // TODO: handle exception
                              e1.printStackTrace();
                          }
                      }
                  }
              }
              

              
          public void testSelect(){
                  List list 
          = session.createQuery(" from TBook").list();
                  
                  Iterator it 
          = list.iterator();
                  
          while (it.hasNext()) {
                      TBook book 
          = (TBook)it.next();
                      System.out.println(book.getName());            
                  }
                  
                  list 
          = session.createQuery(" from TDVD").list();
                  it 
          = list.iterator();
                  
          while (it.hasNext()) {
                      TDVD dvd 
          = (TDVD)it.next();
                      System.out.println(dvd.getName());            
                  }        
              }
          }

          4.查詢結(jié)果

          posted on 2006-07-02 18:32 baim 閱讀(419) 評論(0)  編輯  收藏 所屬分類: 開源軟件框架

          <2006年7月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導航

          常用鏈接

          隨筆分類(27)

          隨筆檔案(28)

          搜索

          最新評論

          主站蜘蛛池模板: 那曲县| 颍上县| 玛曲县| 灵台县| 奉化市| 宝兴县| 尼木县| 苗栗市| 镇原县| 尚志市| 托克逊县| 木兰县| 赤峰市| 关岭| 贵港市| 榆社县| 台江县| 托克托县| 西青区| 荆门市| 青河县| 海伦市| 泸水县| 苏尼特右旗| 颍上县| 康定县| 曲沃县| 天台县| 陆河县| 宣恩县| 沐川县| 天峻县| 秦安县| 曲松县| 达州市| 耒阳市| 宣城市| 阿巴嘎旗| 化隆| 忻城县| 寿光市|