1 package org.hibernate.test.legacy; 3 4 import java.io.Serializable ; 5 6 import junit.framework.Test; 7 import junit.framework.TestSuite; 8 9 import org.hibernate.HibernateException; 10 import org.hibernate.Session; 11 import org.hibernate.Transaction; 12 import org.hibernate.test.TestCase; 13 14 19 public class OneToOneCacheTest extends TestCase { 20 21 public OneToOneCacheTest(String x) { 22 super(x); 23 } 24 25 public static Test suite() { 26 return new TestSuite(OneToOneCacheTest.class); 27 } 28 29 private Serializable generatedId; 30 31 public void testOneToOneCache() throws HibernateException { 32 33 createMainObject(); 35 readMainObject(); 37 38 addObject2(); 40 41 MainObject mainObject = readMainObject(); 44 45 TestCase.assertNotNull(mainObject.getObj2()); 46 47 getSessions().evict(MainObject.class); 49 50 mainObject = readMainObject(); 51 52 TestCase.assertNotNull(mainObject.getObj2()); 53 54 } 55 56 61 private void createMainObject() throws HibernateException { 62 Session session = openSession(); 63 Transaction tx = session.beginTransaction(); 64 65 MainObject mo = new MainObject(); 66 mo.setDescription("Main Test"); 67 68 generatedId = session.save(mo); 69 70 tx.commit(); 71 session.close(); 72 } 73 74 80 private void addObject2() throws HibernateException { 81 Session session = openSession(); 82 Transaction tx = session.beginTransaction(); 83 84 MainObject mo = 85 (MainObject) session.load(MainObject.class, generatedId); 86 87 Object2 toAdd = new Object2(); 88 toAdd.setDummy("test"); 89 90 mo.setObj2(toAdd); 92 93 tx.commit(); 94 session.close(); 95 } 96 97 103 private MainObject readMainObject() throws HibernateException { 104 Long returnId = null; 105 Session session = openSession(); 106 Transaction tx = session.beginTransaction(); 107 108 Serializable id = generatedId; 109 110 MainObject mo = (MainObject) session.load(MainObject.class, id); 111 112 tx.commit(); 113 session.close(); 114 115 return mo; 116 } 117 118 119 122 protected String [] getMappings() { 123 return new String [] { "legacy/Object2.hbm.xml", "legacy/MainObject.hbm.xml" }; 124 } 125 } 126 | Popular Tags |