Hibernate實體層次設計(四)

          Table per class hierarchy

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

          1.數據表如下圖:


          2.數據表定義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.查詢結果

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

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

          導航

          常用鏈接

          隨筆分類(27)

          隨筆檔案(28)

          搜索

          最新評論

          主站蜘蛛池模板: 札达县| 明光市| 南岸区| 大厂| 扎囊县| 文登市| 贺州市| 泽库县| 鹿泉市| 马公市| 平昌县| 巴南区| 开平市| 德清县| 朝阳市| 宜君县| 金川县| 赣榆县| 离岛区| 永修县| 旬邑县| 临城县| 儋州市| 邵武市| 奇台县| 永城市| 肥乡县| 靖江市| 年辖:市辖区| 深州市| 霍林郭勒市| 弋阳县| 翁源县| 正安县| 克拉玛依市| 秦皇岛市| 松江区| 普定县| 益阳市| 利川市| 无棣县|