1 package org.hibernate.test.legacy; 3 4 5 import java.io.Serializable ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import junit.framework.Test; 11 import junit.framework.TestSuite; 12 import junit.textui.TestRunner; 13 import org.hibernate.Hibernate; 14 import org.hibernate.LockMode; 15 import org.hibernate.classic.Session; 16 import org.hibernate.dialect.HSQLDialect; 17 import org.hibernate.test.TestCase; 18 import org.hibernate.Transaction; 19 20 public class ABCProxyTest extends TestCase { 21 22 public ABCProxyTest(String arg0) { 23 super(arg0); 24 } 25 26 public void testDiscriminatorFiltering() throws Exception { 27 if ( ( getDialect() instanceof HSQLDialect ) ) return; 28 Session s = openSession(); 29 Transaction t = s.beginTransaction(); 30 s.createQuery("from C1 c1 left join c1.c2s c2").list(); 31 s.createCriteria(C1.class).createCriteria("c2s").list(); 32 t.commit(); 33 s.close(); 34 } 35 36 public void testNarrow() throws Exception { 37 Session s = openSession(); 38 Transaction t = s.beginTransaction(); 39 s.createQuery("from E e join e.reverse as b where b.count=1").list(); 40 s.createQuery("from E e join e.as as b where b.count=1").list(); 41 t.commit(); 42 s.close(); 43 } 44 45 public void testSharedColumn() throws Exception { 46 Session s = openSession(); 47 Transaction t = s.beginTransaction(); 48 C1 c1 = new C1(); 49 C2 c2 = new C2(); 50 c1.setC2(c2); 51 c2.setC1(c1); 52 s.save(c1); s.save(c2); 53 t.commit(); 54 s.close(); 55 56 s = openSession(); 57 t = s.beginTransaction(); 58 List list = s.find("from B"); 59 assertTrue( list.size()==2 ); 60 t.commit(); 61 s.close(); 62 63 s = openSession(); 64 t = s.beginTransaction(); 65 c1 = (C1) s.createQuery("from C1").uniqueResult(); 66 c2 = (C2) s.createQuery("from C2").uniqueResult(); 67 assertTrue( c1.getC2()==c2 ); 68 assertTrue( c2.getC1()==c1 ); 69 assertTrue( c1.getC2s().contains(c2) ); 70 assertTrue( c2.getC1s().contains(c1) ); 71 t.commit(); 72 s.close(); 73 74 s = openSession(); 75 t = s.beginTransaction(); 76 c1 = (C1) s.get( A.class, c1.getId() ); 77 c2 = (C2) s.get( A.class, c2.getId() ); 78 assertTrue( c1.getC2()==c2 ); 79 assertTrue( c2.getC1()==c1 ); 80 assertTrue( c1.getC2s().contains(c2) ); 81 assertTrue( c2.getC1s().contains(c1) ); 82 t.commit(); 83 s.close(); 84 85 s = openSession(); 86 t = s.beginTransaction(); 87 s.delete(c1); s.delete(c2); 88 t.commit(); 89 s.close(); 90 91 } 92 93 public void testSubclassing() throws Exception { 94 Session s = openSession(); 95 Transaction t = s.beginTransaction(); 96 C1 c1 = new C1(); 97 D d = new D(); 98 d.setAmount(213.34f); 99 c1.setAddress("foo bar"); 100 c1.setCount(23432); 101 c1.setName("c1"); 102 c1.setD(d); 103 s.save(c1); 104 d.setId( c1.getId() ); 105 s.save(d); 106 t.commit(); 107 s.close(); 108 109 s = openSession(); 110 t = s.beginTransaction(); 111 A c1a = (A) s.load( A.class, c1.getId() ); 112 assertFalse( Hibernate.isInitialized(c1a) ); 113 assertTrue( c1a.getName().equals("c1") ); 114 t.commit(); 115 s.close(); 116 117 s = openSession(); 118 t = s.beginTransaction(); 119 B c1b = (B) s.load( B.class, c1.getId() ); 120 assertTrue( 121 (c1b.getCount()==23432) && 122 c1b.getName().equals("c1") 123 ); 124 t.commit(); 125 s.close(); 126 127 s = openSession(); 128 t = s.beginTransaction(); 129 c1 = (C1) s.load( C1.class, c1.getId() ); 130 assertTrue( 131 c1.getAddress().equals("foo bar") && 132 (c1.getCount()==23432) && 133 c1.getName().equals("c1") && 134 c1.getD().getAmount()>213.3f 135 ); 136 t.commit(); 137 s.close(); 138 139 s = openSession(); 140 t = s.beginTransaction(); 141 c1a = (A) s.load( A.class, c1.getId() ); 142 assertTrue( c1a.getName().equals("c1") ); 143 c1 = (C1) s.load( C1.class, c1.getId() ); 144 assertTrue( 145 c1.getAddress().equals("foo bar") && 146 (c1.getCount()==23432) && 147 c1.getName().equals("c1") && 148 c1.getD().getAmount()>213.3f 149 ); 150 c1b = (B) s.load( B.class, c1.getId() ); 151 assertTrue( 152 (c1b.getCount()==23432) && 153 c1b.getName().equals("c1") 154 ); 155 assertTrue( c1a.getName().equals("c1") ); 156 t.commit(); 157 s.close(); 158 159 s = openSession(); 160 t = s.beginTransaction(); 161 c1a = (A) s.load( A.class, c1.getId() ); 162 assertTrue( c1a.getName().equals("c1") ); 163 c1 = (C1) s.load( C1.class, c1.getId(), LockMode.UPGRADE ); 164 assertTrue( 165 c1.getAddress().equals("foo bar") && 166 (c1.getCount()==23432) && 167 c1.getName().equals("c1") && 168 c1.getD().getAmount()>213.3f 169 ); 170 c1b = (B) s.load( B.class, c1.getId(), LockMode.UPGRADE ); 171 assertTrue( 172 (c1b.getCount()==23432) && 173 c1b.getName().equals("c1") 174 ); 175 assertTrue( c1a.getName().equals("c1") ); 176 t.commit(); 177 s.close(); 178 179 s = openSession(); 180 t = s.beginTransaction(); 181 c1a = (A) s.load( A.class, c1.getId() ); 182 c1 = (C1) s.load( C1.class, c1.getId() ); 183 c1b = (B) s.load( B.class, c1.getId() ); 184 assertTrue( c1a.getName().equals("c1") ); 185 assertTrue( 186 c1.getAddress().equals("foo bar") && 187 (c1.getCount()==23432) && 188 c1.getName().equals("c1") && 189 c1.getD().getAmount()>213.3f 190 ); 191 assertTrue( 192 (c1b.getCount()==23432) && 193 c1b.getName().equals("c1") 194 ); 195 System.out.println( s.delete("from A") ); 196 t.commit(); 197 s.close(); 198 199 s = openSession(); 200 t = s.beginTransaction(); 201 s.save( new B() ); 202 s.save( new A() ); 203 assertTrue( s.find("from B").size()==1 ); 204 assertTrue( s.find("from A").size()==2 ); 205 s.delete("from A"); 206 t.commit(); 207 s.close(); 208 } 209 210 public void testSubclassMap() throws Exception { 211 Session s = openSession(); 212 Transaction t = s.beginTransaction(); 213 B b = new B(); 214 s.save(b); 215 Map map = new HashMap (); 216 map.put("3", new Integer (1) ); 217 b.setMap(map); 218 s.flush(); 219 s.delete(b); 220 t.commit(); 221 s.close(); 222 223 s = openSession(); 224 t = s.beginTransaction(); 225 map = new HashMap (); 226 map.put("3", new Integer (1) ); 227 b = new B(); 228 b.setMap(map); 229 s.save(b); 230 s.flush(); 231 s.delete(b); 232 t.commit(); 233 s.close(); 234 } 235 236 public void testOneToOne() throws Exception { 237 Session s = openSession(); 238 Transaction t = s.beginTransaction(); 239 A a = new A(); 240 E d1 = new E(); 241 C1 c = new C1(); 242 E d2 = new E(); 243 a.setForward(d1); 244 d1.setReverse(a); 245 c.setForward(d2); 246 d2.setReverse(c); 247 Serializable aid = s.save(a); 248 Serializable d2id = s.save(d2); 249 t.commit(); 250 s.close(); 251 252 s = openSession(); 253 t = s.beginTransaction(); 254 List l = s.find( "from E e, A a where e.reverse = a.forward and a = ?", a, Hibernate.entity(A.class) ); 255 assertTrue( l.size()==1 ); 256 l = s.find( "from E e join fetch e.reverse" ); 257 assertTrue( l.size()==2 ); 258 t.commit(); 259 s.close(); 260 261 s = openSession(); 262 t = s.beginTransaction(); 263 l = s.find( "from E e" ); 264 assertTrue( l.size()==2 ); 265 E e = (E) l.get(0); 266 assertTrue( e==e.getReverse().getForward() ); 267 e = (E) l.get(1); 268 assertTrue( e==e.getReverse().getForward() ); 269 t.commit(); 270 s.close(); 271 272 s = openSession(); 273 t = s.beginTransaction(); 274 a = (A) s.load(A.class, aid); 275 d2 = (E) s.load(E.class, d2id); 276 assertTrue( a==a.getForward().getReverse() ); 277 assertTrue( d2==d2.getReverse().getForward() ); 278 s.delete(a); 279 s.delete( a.getForward() ); 280 s.delete(d2); 281 s.delete( d2.getReverse() ); 282 t.commit(); 283 284 s = openSession(); 285 t = s.beginTransaction(); 286 l = s.find( "from E e" ); 287 assertTrue( l.size()==0 ); 288 t.commit(); 289 s.close(); 290 } 291 292 public String [] getMappings() { 293 return new String [] { "legacy/ABCProxy.hbm.xml" }; 294 } 295 296 public static Test suite() { 297 return new TestSuite(ABCProxyTest.class); 298 } 299 300 public static void main(String [] args) throws Exception { 301 TestRunner.run( suite() ); 302 } 303 304 } 305 | Popular Tags |