1 | public void testHandSQL() { | | 1 | public void testIterateWithEvictBottomOfLoop() {
|
2 | Session s = openSession(); | | 2 | Session s = openSession();
|
3 | Transaction t = s.beginTransaction(); | | 3 | s.beginTransaction();
|
4 | Organization ifa = new Organization( "IFA" ); | | 4 | Set parents = new HashSet();
|
5 | Organization jboss = new Organization( "JBoss" ); | | 5 | for (int i=0; i<5; i++) {
|
6 | Person gavin = new Person( "Gavin" ); | | 6 | Parent p = new Parent( String.valueOf( i + 100 ) );
|
7 | Employment emp = new Employment( gavin, jboss, "AU" ); | | 7 | Child child = new Child( "child" + i );
|
8 | Serializable orgId = s.save( jboss ); | | 8 | child.setParent( p );
|
9 | s.save( ifa ); | | 9 | p.getChildren().add( child );
|
10 | s.save( gavin ); | | 10 | s.save( p );
|
11 | s.save( emp ); | | 11 | parents.add(p);
|
12 | t.commit(); | | 12 | }
|
13 | | | 13 | s.getTransaction().commit();
|
14 | t = s.beginTransaction(); | | 14 | s.close();
|
15 | Person christian = new Person( "Christian" ); | | 15 |
|
16 | s.save( christian ); | | 16 | s = openSession();
|
17 | Employment emp2 = new Employment( christian, jboss, "EU" ); | | 17 | s.beginTransaction();
|
18 | s.save( emp2 ); | | 18 | for (Iterator it = s.createQuery( "from Parent" ).iterate(); it.hasNext(); ) {
|
19 | t.commit(); | | 19 | Parent p = (Parent) it.next();
|
20 | s.close(); | | 20 | assertEquals( 1, p.getChildren().size() );
|
21 | | | 21 | s.evict(p);
|
22 | getSessions().evict( Organization.class ); | | 22 | }
|
23 | getSessions().evict( Person.class ); | | 23 | s.getTransaction().commit();
|
24 | getSessions().evict( Employment.class ); | | 24 | s.close();
|
25 | | | 25 |
|
26 | s = openSession(); | | 26 | s = openSession();
|
27 | t = s.beginTransaction(); | | 27 | s.beginTransaction();
|
28 | jboss = ( Organization ) s.get( Organization.class, orgId ); | | 28 | for (Iterator it=parents.iterator(); it.hasNext(); ) {
|
29 | assertEquals( jboss.getEmployments().size(), 2 ); | | 29 | s.delete(it.next());
|
30 | assertEquals( jboss.getName(), "JBOSS" ); | | 30 | }
|
31 | emp = ( Employment ) jboss.getEmployments().iterator().next(); | | 31 | s.getTransaction().commit();
|
32 | gavin = emp.getEmployee(); | | 32 | s.close();
|
33 | assertEquals( gavin.getName(), "GAVIN" ); | | 33 | } |
34 | assertEquals( s.getCurrentLockMode( gavin ), LockMode.UPGRADE ); | | | |
35 | emp.setEndDate( new Date() ); | | | |
36 | Employment emp3 = new Employment( gavin, jboss, "US" ); | | | |
37 | s.save( emp3 ); | | | |
38 | t.commit(); | | | |
39 | s.close(); | | | |
40 | | | | |
41 | s = openSession(); | | | |
42 | t = s.beginTransaction(); | | | |
43 | Iterator iter = s.getNamedQuery( "allOrganizationsWithEmployees" ).list().iterator(); | | | |
44 | assertTrue( iter.hasNext() ); | | | |
45 | Organization o = ( Organization ) iter.next(); | | | |
46 | assertEquals( o.getEmployments().size(), 3 ); | | | |
47 | Iterator iter2 = o.getEmployments().iterator(); | | | |
48 | while ( iter2.hasNext() ) { | | | |
49 | Employment e = ( Employment ) iter2.next(); | | | |
50 | s.delete( e ); | | | |
51 | } | | | |
52 | iter2 = o.getEmployments().iterator(); | | | |
53 | while ( iter2.hasNext() ) { | | | |
54 | Employment e = ( Employment ) iter2.next(); | | | |
55 | s.delete( e.getEmployee() ); | | | |
56 | } | | | |
57 | s.delete( o ); | | | |
58 | assertFalse( iter.hasNext() ); | | | |
59 | s.delete( ifa ); | | | |
60 | t.commit(); | | | |
61 | s.close(); | | | |
62 | } | | | |