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>