1 package org.apache.ojb.broker; 2 3 import java.util.Collection ; 4 5 import org.apache.ojb.broker.core.proxy.ProxyHelper; 6 import org.apache.ojb.broker.metadata.ClassDescriptor; 7 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; 8 import org.apache.ojb.broker.query.Criteria; 9 import org.apache.ojb.broker.query.Query; 10 import org.apache.ojb.broker.query.QueryFactory; 11 import org.apache.ojb.junit.PBTestCase; 12 13 27 public class PrimaryKeyForeignKeyTest extends PBTestCase 28 { 29 public static void main(String [] args) 30 { 31 String [] arr = {PrimaryKeyForeignKeyTest.class.getName()}; 32 junit.textui.TestRunner.main(arr); 33 } 34 35 public void tearDown() throws Exception 36 { 37 try 38 { 39 if(broker != null) 40 { 41 changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, false); 42 changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, false); 43 changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, false); 44 } 45 } 46 finally 47 { 48 super.tearDown(); 49 } 50 } 51 52 public void testStoreLookup() 53 { 54 changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, false); 55 changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, false); 56 changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, false); 57 58 59 String postfix = "_" + System.currentTimeMillis(); 60 61 broker.beginTransaction(); 62 Person jeff = new Person("jeff" + postfix, null); 63 broker.store(jeff); 65 GenderIF gender = new Gender("male"+postfix); 66 PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(), "profile"+postfix, jeff, gender); 67 broker.store(gender); 68 broker.serviceBrokerHelper().link(jeffDetail, true); 69 broker.store(jeffDetail); 70 jeff.setDetail(jeffDetail); 72 broker.store(jeff); 73 broker.commitTransaction(); 74 75 broker.clearCache(); 76 77 Criteria crit = new Criteria(); 78 crit.addEqualTo("name", "jeff" + postfix); 79 Query query = QueryFactory.newQuery(Person.class, crit); 80 Collection result = broker.getCollectionByQuery(query); 81 assertEquals(1, result.size()); 82 Person jeffNew = (Person) result.iterator().next(); 83 assertEquals("jeff" + postfix, jeffNew.getName()); 84 assertNotNull(jeffNew.getDetail()); 85 assertFalse(ProxyHelper.isProxy(jeffNew.getDetail())); 86 PersonDetailIF jeffNewDetail = jeffNew.getDetail(); 87 assertNotNull(jeffNewDetail.getGender()); 88 assertEquals("male"+postfix, jeffNewDetail.getGender().getType()); 89 90 broker.beginTransaction(); 91 broker.delete(jeffNew); 92 GenderIF g = jeffNewDetail.getGender(); 93 broker.delete(jeffNewDetail); 94 broker.delete(g); 95 broker.commitTransaction(); 96 } 97 98 public void testStoreLookupProxy() 99 { 100 changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, true); 101 changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, true); 102 changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE, true); 103 104 105 String postfix = "_" + System.currentTimeMillis(); 106 107 broker.beginTransaction(); 108 Person jeff = new Person("jeff" + postfix, null); 109 broker.store(jeff); 111 GenderIF gender = new Gender("male"+postfix); 112 PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(), "profile"+postfix, jeff, gender); 113 broker.store(gender); 114 broker.serviceBrokerHelper().link(jeffDetail, true); 115 broker.store(jeffDetail); 116 jeff.setDetail(jeffDetail); 118 broker.store(jeff); 119 broker.commitTransaction(); 120 121 broker.clearCache(); 122 123 Criteria crit = new Criteria(); 124 crit.addEqualTo("name", "jeff" + postfix); 125 Query query = QueryFactory.newQuery(Person.class, crit); 126 Collection result = broker.getCollectionByQuery(query); 127 assertEquals(1, result.size()); 128 Person jeffNew = (Person) result.iterator().next(); 129 assertEquals("jeff" + postfix, jeffNew.getName()); 130 assertNotNull(jeffNew.getDetail()); 131 assertTrue(ProxyHelper.isProxy(jeffNew.getDetail())); 132 PersonDetailIF jeffNewDetail = jeffNew.getDetail(); 133 assertNotNull(jeffNewDetail.getGender()); 134 assertTrue(ProxyHelper.isProxy(jeffNewDetail.getGender())); 135 assertEquals("male"+postfix, jeffNewDetail.getGender().getType()); 136 137 broker.beginTransaction(); 138 broker.delete(jeffNew); 139 GenderIF g = jeffNewDetail.getGender(); 140 broker.delete(jeffNewDetail); 141 broker.delete(g); 142 broker.commitTransaction(); 143 } 144 145 public void testStoreLookup_2() 146 { 147 changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT, false); 148 changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT, false); 149 changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT, false); 150 151 152 String postfix = "_" + System.currentTimeMillis(); 153 154 broker.beginTransaction(); 155 Person jeff = new Person("jeff" + postfix, null); 156 broker.store(jeff); 158 GenderIF gender = new Gender("male"+postfix); 159 PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(), "profile"+postfix, jeff, gender); 160 broker.store(jeffDetail); 161 jeff.setDetail(jeffDetail); 163 broker.store(jeff); 164 broker.commitTransaction(); 165 166 broker.clearCache(); 167 168 Criteria crit = new Criteria(); 169 crit.addEqualTo("name", "jeff" + postfix); 170 Query query = QueryFactory.newQuery(Person.class, crit); 171 Collection result = broker.getCollectionByQuery(query); 172 assertEquals(1, result.size()); 173 Person jeffNew = (Person) result.iterator().next(); 174 assertEquals("jeff" + postfix, jeffNew.getName()); 175 assertNotNull(jeffNew.getDetail()); 176 assertFalse(ProxyHelper.isProxy(jeffNew.getDetail())); 177 PersonDetailIF jeffNewDetail = jeffNew.getDetail(); 178 assertNotNull(jeffNewDetail.getGender()); 179 assertEquals("male"+postfix, jeffNewDetail.getGender().getType()); 180 181 broker.beginTransaction(); 182 broker.delete(jeffNew); 183 GenderIF g = jeffNewDetail.getGender(); 184 broker.delete(jeffNewDetail); 185 broker.delete(g); 186 broker.commitTransaction(); 187 } 188 189 public void testStoreLookupProxy_2() 190 { 191 changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT, true); 192 changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT, true); 193 changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT, true); 194 195 196 String postfix = "_" + System.currentTimeMillis(); 197 198 broker.beginTransaction(); 199 Person jeff = new Person("jeff" + postfix, null); 200 broker.store(jeff); 202 GenderIF gender = new Gender("male"+postfix); 203 PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(), "profile"+postfix, jeff, gender); 204 broker.store(jeffDetail); 205 jeff.setDetail(jeffDetail); 207 broker.store(jeff); 208 broker.commitTransaction(); 209 210 broker.clearCache(); 211 212 Criteria crit = new Criteria(); 213 crit.addEqualTo("name", "jeff" + postfix); 214 Query query = QueryFactory.newQuery(Person.class, crit); 215 Collection result = broker.getCollectionByQuery(query); 216 assertEquals(1, result.size()); 217 Person jeffNew = (Person) result.iterator().next(); 218 assertEquals("jeff" + postfix, jeffNew.getName()); 219 assertNotNull(jeffNew.getDetail()); 220 assertTrue(ProxyHelper.isProxy(jeffNew.getDetail())); 221 PersonDetailIF jeffNewDetail = jeffNew.getDetail(); 222 assertNotNull(jeffNewDetail.getGender()); 223 assertTrue(ProxyHelper.isProxy(jeffNewDetail.getGender())); 224 assertEquals("male"+postfix, jeffNewDetail.getGender().getType()); 225 226 broker.beginTransaction(); 227 broker.delete(jeffNew); 228 GenderIF g = jeffNewDetail.getGender(); 229 broker.delete(jeffNewDetail); 230 broker.delete(g); 231 broker.commitTransaction(); 232 } 233 234 private void changeRepositoryAutoSetting(Class clazz, String attributeName, boolean retrieve, int update, int delete, boolean proxy) 235 { 236 ClassDescriptor cld = broker.getClassDescriptor(clazz); 237 ObjectReferenceDescriptor ord = cld.getObjectReferenceDescriptorByName(attributeName); 238 ord.setCascadeRetrieve(retrieve); 239 ord.setCascadingStore(update); 240 ord.setCascadingDelete(delete); 241 ord.setLazy(proxy); 242 } 243 244 public static class Person 245 { 246 Integer id; 247 String name; 248 PersonDetailIF detail; 249 250 public Person() 251 { 252 } 253 254 public Person(String name, PersonDetailIF detail) 255 { 256 this.name = name; 257 this.detail = detail; 258 } 259 260 public Integer getId() 261 { 262 return id; 263 } 264 265 public void setId(Integer id) 266 { 267 this.id = id; 268 } 269 270 public String getName() 271 { 272 return name; 273 } 274 275 public void setName(String name) 276 { 277 this.name = name; 278 } 279 280 public PersonDetailIF getDetail() 281 { 282 return detail; 283 } 284 285 public void setDetail(PersonDetailIF detail) 286 { 287 this.detail = detail; 288 } 289 } 290 291 public static interface PersonDetailIF 292 { 293 public Integer getId(); 294 public void setId(Integer id); 295 public String getProfile(); 296 public void setProfile(String profile); 297 public Person getPerson(); 298 public void setPerson(Person person); 299 public GenderIF getGender(); 300 public void setGender(GenderIF person); 301 } 302 303 public static class PersonDetail implements PersonDetailIF 304 { 305 Integer id; 306 String profile; 307 Person person; 308 GenderIF gender; 309 310 public PersonDetail() 311 { 312 } 313 314 public PersonDetail(Integer id, String profile, Person person, GenderIF gender) 315 { 316 this.id = id; 317 this.profile = profile; 318 this.person = person; 319 this.gender = gender; 320 } 321 322 public Integer getId() 323 { 324 return id; 325 } 326 327 public void setId(Integer id) 328 { 329 this.id = id; 330 } 331 332 public String getProfile() 333 { 334 return profile; 335 } 336 337 public void setProfile(String profile) 338 { 339 this.profile = profile; 340 } 341 342 public Person getPerson() 343 { 344 return person; 345 } 346 347 public void setPerson(Person person) 348 { 349 this.person = person; 350 } 351 352 public GenderIF getGender() 353 { 354 return gender; 355 } 356 357 public void setGender(GenderIF gender) 358 { 359 this.gender = gender; 360 } 361 } 362 363 public static interface GenderIF 364 { 365 public String getType(); 366 public void setType(String type); 367 } 368 369 public static class Gender implements GenderIF 370 { 371 String type; 372 373 public Gender() 374 { 375 } 376 377 public Gender(String type) 378 { 379 this.type = type; 380 } 381 382 public String getType() 383 { 384 return type; 385 } 386 387 public void setType(String type) 388 { 389 this.type = type; 390 } 391 } 392 } 393 | Popular Tags |