<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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
    這個錯嗎?  回復  更多評論
      


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


    網站導航:
     
    主站蜘蛛池模板: 免费在线中文日本| 国产一级一毛免费黄片| 国产精品69白浆在线观看免费| 国产成人亚洲综合无码精品 | 最新国产AV无码专区亚洲 | 二个人看的www免费视频| 免费A级毛片无码A| 美女黄色毛片免费看| 国产成人免费一区二区三区| 国产亚洲人成在线影院| 久久亚洲欧洲国产综合| 最近免费中文字幕中文高清| 亚洲午夜视频在线观看| 最近免费中文字幕大全免费 | 色屁屁在线观看视频免费| 亚洲国产精品碰碰| 在线涩涩免费观看国产精品| 91亚洲va在线天线va天堂va国产| 亚洲一区二区免费视频| 亚洲国产无线乱码在线观看 | 成人毛片免费视频| 美女被免费网站视频在线| 亚洲av无码成人精品区| 精品国产福利尤物免费| 内射少妇36P亚洲区| 四色在线精品免费观看| 男女拍拍拍免费视频网站| 亚洲高清日韩精品第一区| 热久久精品免费视频| 羞羞视频免费网站在线看| 久久久亚洲欧洲日产国码二区| 成年女人午夜毛片免费视频| 成人久久久观看免费毛片| 亚洲视频精品在线观看| 四虎影院永久免费观看| 无码人妻AV免费一区二区三区| 亚洲AV无码一区二区三区牛牛| 亚洲性日韩精品一区二区三区| 久久午夜夜伦鲁鲁片免费无码影视| 亚洲AV无码一区二区一二区| 亚洲av一综合av一区|