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


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


    網站導航:
     
    主站蜘蛛池模板: 麻豆亚洲AV成人无码久久精品 | 亚洲国产成人久久综合一区77| 亚洲网红精品大秀在线观看| 国产免费一级高清淫曰本片 | 国产hs免费高清在线观看| 亚洲依依成人亚洲社区| 青春禁区视频在线观看直播免费 | 亚洲gay片在线gv网站| 国产男女猛烈无遮挡免费视频网站| 亚洲中文精品久久久久久不卡| 国产精品视频免费一区二区| 亚洲熟妇久久精品| 国产嫩草影院精品免费网址| 高清免费久久午夜精品| 亚洲欧洲精品无码AV| 中文字幕免费观看| 97se亚洲国产综合自在线| A级毛片内射免费视频| 国产亚洲精品AAAA片APP| 亚洲一级片内射网站在线观看| fc2成年免费共享视频18| 亚洲AV无码一区二区三区DV| 69影院毛片免费观看视频在线| 国产亚洲国产bv网站在线| 日本高清免费aaaaa大片视频| 一边摸一边桶一边脱免费视频 | 亚洲日韩在线中文字幕第一页 | 成人毛片18女人毛片免费96| 青娱乐在线免费观看视频| 国产亚洲综合久久系列| 99久久久精品免费观看国产| 黄色免费网址在线观看| 亚洲AV日韩AV天堂久久| 女人18毛片免费观看| 国产免费福利体检区久久| 亚洲专区中文字幕| 91麻豆国产自产在线观看亚洲| 日韩在线播放全免费| 伊人久久国产免费观看视频| 亚洲国产精品乱码在线观看97| 亚洲AV网站在线观看|