My-java-spark

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            5 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

                 Hibernate的動態模型為我們動態改動表結構帶來了方便, 個人認為這一點非常有價值, 現在的企業級應用系統越來越強調用戶可定制性, hibernate的這一點使用戶自定義字段或自定義表成為可能 .
          關于動態模型, 我還是把hibernate自帶的測試用例貼到這里, 用以備忘.

          java代碼:

          //$Id: DynamicClassTest.java,v 1.4 2005/03/06 16:31:24 oneovthafew Exp $
          package org.hibernate.test.dynamic;

          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.Iterator;
          import java.util.List;
          import java.util.Map;

          import junit.framework.Test;
          import junit.framework.TestSuite;

          import org.hibernate.EntityMode;
          import org.hibernate.Hibernate;
          import org.hibernate.Session;
          import org.hibernate.Transaction;
          import org.hibernate.cfg.Configuration;
          import org.hibernate.cfg.Environment;
          import org.hibernate.test.TestCase;

          /**
           * @author Gavin King
           */
          public class DynamicClassTest extends TestCase {
           
           public DynamicClassTest(String str) {
            super(str);
           }

           protected void configure(Configuration cfg) {
            cfg.setProperty(Environment.DEFAULT_ENTITY_MODE, EntityMode.MAP.toString());
           }

           public void testLazyDynamicClass() {
            Session s = openSession();
            assertTrue( "Incorrectly handled default_entity_mode", s.getEntityMode() == EntityMode.MAP );
            Session other = s.getSession( EntityMode.MAP );
            assertEquals( "openSession() using same entity-mode returned new session", s, other );

            other = s.getSession( EntityMode.POJO );
            other.close();
            assertTrue( !other.isOpen() );
            assertTrue( other.isConnected() );  // because it is linked to the "root" session's connection

            s.close();

            s = openSession();
            Transaction t = s.beginTransaction();

            Map cars = new HashMap();
            cars.put("description", "Cars");
            Map monaro = new HashMap();
            monaro.put("productLine", cars);
            monaro.put("name", "monaro");
            monaro.put("description", "Holden Monaro");
            Map hsv = new HashMap();
            hsv.put("productLine", cars);
            hsv.put("name", "hsv");
            hsv.put("description", "Holden Commodore HSV");
            List models = new ArrayList();
            cars.put("models", models);
            models.add(hsv);
            models.add(monaro);
            s.save("ProductLine", cars);
            t.commit();
            s.close();

            s = openSession();
            t = s.beginTransaction();
            
            cars = (Map) s.createQuery("from ProductLine pl order by pl.description").uniqueResult();
            models = (List) cars.get("models");
            assertFalse( Hibernate.isInitialized(models) );
            assertEquals( models.size(), 2);
            assertTrue( Hibernate.isInitialized(models) );
            
            s.clear();
            
            List list = s.createQuery("from Model m").list();
            for ( Iterator i=list.iterator(); i.hasNext(); ) {
             assertFalse( Hibernate.isInitialized( ( (Map) i.next() ).get("productLine") ) );
            }
            Map model = (Map) list.get(0);
            assertTrue( ( (List) ( (Map) model.get("productLine") ).get("models") ).contains(model) );
            s.clear();
            
            t.commit();
            s.close();

            s = openSession();
            t = s.beginTransaction();
            cars = (Map) s.createQuery("from ProductLine pl order by pl.description").uniqueResult();
            s.delete(cars);
            t.commit();
            s.close();
           }


           protected String[] getMappings() {
            return new String[] { "dynamic/ProductLine.hbm.xml" };
           }

           public static Test suite() {
            return new TestSuite(DynamicClassTest.class);
           }

          }

          配置文件:

          <?xml version="1.0"?>
          <!DOCTYPE hibernate-mapping PUBLIC
           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
           "

          <hibernate-mapping>

          <!--

            This mapping demonstrates "dynamic" entities.
              
          -->

           <class entity-name="ProductLine">

               <id name="id"
                column="productId"
                length="32"
                type="string">
                <generator class="uuid.hex"/>
               </id>

               <property name="description"
                not-null="true"
                length="200"
                type="string"/>

               <!-- don't use sets for associations, unless you want stack overflows! -->
               <!--這一點要特別小心, 我剛開始做試驗的時候用的就是Set, 結果拋出 stack overflows異常, 害的我兩個小時搞不定, 最后還是看了這個test, 才知道用這樣的限制-->

               <bag name="models"
                 cascade="all"
                 inverse="true">
                <key column="productId"/>
                <one-to-many class="Model"/>
               </bag>

           </class>

              <class entity-name="Model">

               <id name="id"
                column="modelId"
                length="32"
                type="string">
                <generator class="uuid.hex"/>
               </id>
               
               <property name="name"
                not-null="true"
                length="25"
                type="string"/>
                
               <property name="description"
                not-null="true"
                length="200"
                type="string"/>
               
               <many-to-one name="productLine"
                column="productId"
                not-null="true"
                class="ProductLine"/>
               
           </class>

          </hibernate-mapping>


          評論

          # re: Hibernate動態模型(dynamic models) 一對多映射的實現 2008-03-17 20:19 yeti2001
          <hibernate-mapping>
          <class name="hib.TtItem" table="TT_ITEM" schema="SCOTT">
          <id name="id" type="java.lang.Long">
          <column name="ID" precision="22" />
          <generator class="increment" />
          </id>
          <property name="name" type="java.lang.String">
          <column name="NAME" length="80" />
          </property>
          <joined-subclass entity-name="Ttbook">
          <key column="aid" />
          <property name="ttt" type="java.lang.String"></property>
          </joined-subclass>
          </class>
          </hibernate-mapping>
          有沒有試驗過這樣的配置文件
          我的email:yeti20022@hotmail.com
            回復  更多評論
            

          # re: Hibernate動態模型(dynamic models) 一對多映射的實現[未登錄] 2013-07-15 11:38 cloud
          請問你這個例子在這一步
          List models = new ArrayList();
          cars.put("models", models);
          的時候,最后save的時候不會出現
          java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
          這個錯嗎?  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 顺昌县| 玉屏| 攀枝花市| 凌源市| 沈阳市| 江永县| 宁陕县| 盘山县| 松江区| 荆州市| 东源县| 酉阳| 时尚| 察哈| 迁安市| 宁武县| 甘德县| 尼勒克县| 蒙城县| 徐州市| 连山| 景洪市| 茌平县| 科技| 南雄市| 榆树市| 平利县| 东台市| 黄龙县| 海盐县| 瓮安县| 定襄县| 贺州市| 张家口市| 遂川县| 雷波县| 林芝县| 阿瓦提县| 嘉兴市| 祁阳县| 永顺县|