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"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<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>

posted on 2005-09-12 22:11 spark 阅读(5563) 评论(2)  编辑  收藏

评论

# 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
这个错吗?  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: