1 package org.hibernate.test.dynamic; 3 4 import java.util.ArrayList ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import junit.framework.Test; 11 import junit.framework.TestSuite; 12 13 import org.hibernate.EntityMode; 14 import org.hibernate.Hibernate; 15 import org.hibernate.Session; 16 import org.hibernate.Transaction; 17 import org.hibernate.cfg.Configuration; 18 import org.hibernate.cfg.Environment; 19 import org.hibernate.test.TestCase; 20 21 24 public class DynamicClassTest extends TestCase { 25 26 public DynamicClassTest(String str) { 27 super(str); 28 } 29 30 protected void configure(Configuration cfg) { 31 cfg.setProperty(Environment.DEFAULT_ENTITY_MODE, EntityMode.MAP.toString()); 32 } 33 34 public void testLazyDynamicClass() { 35 Session s = openSession(); 36 assertTrue( "Incorrectly handled default_entity_mode", s.getEntityMode() == EntityMode.MAP ); 37 Session other = s.getSession( EntityMode.MAP ); 38 assertEquals( "openSession() using same entity-mode returned new session", s, other ); 39 40 other = s.getSession( EntityMode.POJO ); 41 other.close(); 42 assertTrue( !other.isOpen() ); 43 assertTrue( other.isConnected() ); 45 s.close(); 46 47 s = openSession(); 48 Transaction t = s.beginTransaction(); 49 50 Map cars = new HashMap (); 51 cars.put("description", "Cars"); 52 Map monaro = new HashMap (); 53 monaro.put("productLine", cars); 54 monaro.put("name", "monaro"); 55 monaro.put("description", "Holden Monaro"); 56 Map hsv = new HashMap (); 57 hsv.put("productLine", cars); 58 hsv.put("name", "hsv"); 59 hsv.put("description", "Holden Commodore HSV"); 60 List models = new ArrayList (); 61 cars.put("models", models); 62 models.add(hsv); 63 models.add(monaro); 64 s.save("ProductLine", cars); 65 t.commit(); 66 s.close(); 67 68 s = openSession(); 69 t = s.beginTransaction(); 70 71 cars = (Map ) s.createQuery("from ProductLine pl order by pl.description").uniqueResult(); 72 models = (List ) cars.get("models"); 73 assertFalse( Hibernate.isInitialized(models) ); 74 assertEquals( models.size(), 2); 75 assertTrue( Hibernate.isInitialized(models) ); 76 77 s.clear(); 78 79 List list = s.createQuery("from Model m").list(); 80 for ( Iterator i=list.iterator(); i.hasNext(); ) { 81 assertFalse( Hibernate.isInitialized( ( (Map ) i.next() ).get("productLine") ) ); 82 } 83 Map model = (Map ) list.get(0); 84 assertTrue( ( (List ) ( (Map ) model.get("productLine") ).get("models") ).contains(model) ); 85 s.clear(); 86 87 t.commit(); 88 s.close(); 89 90 s = openSession(); 91 t = s.beginTransaction(); 92 cars = (Map ) s.createQuery("from ProductLine pl order by pl.description").uniqueResult(); 93 s.delete(cars); 94 t.commit(); 95 s.close(); 96 } 97 98 99 protected String [] getMappings() { 100 return new String [] { "dynamic/ProductLine.hbm.xml" }; 101 } 102 103 public static Test suite() { 104 return new TestSuite(DynamicClassTest.class); 105 } 106 107 } 108 109 | Popular Tags |