1 | public void testRemoveClear() {↵ | | 1 | public void testLazy() throws Exception {↵
|
2 | Session s = openSession();↵ | | 2 | Session s = openSession();↵
|
3 | Transaction t = s.beginTransaction();↵ | | 3 | Transaction t = s.beginTransaction();↵
|
4 | User gavin = new User("gavin", "secret");↵ | | 4 | Person p = new Person("Gavin"↵
|
5 | User turin = new User("turin", "tiger");↵ | | |
|
6 | Group g = new Group("developers");↵ | | |
|
7 | g.getUsers().put("gavin", gavin);↵ | | |
|
8 | g.getUsers().put("turin", turin);↵ | | |
|
9 | s.persist(g);↵ | | |
|
10 | gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") );↵ | | |
|
11 | gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") ↵ | | 5 | );↵
|
| | | 6 | Person p2 = new Person("Emmanuel");↵
|
| | | 7 | Employee e = new Employee(p);↵
|
| | | 8 | new Employment(e, "JBoss");↵
|
| | | 9 | Employment old = new Employment(e, "IFA");↵
|
| | | 10 | old.setEndDate( new Date() );↵
|
| | | 11 | s.persist(p);↵
|
12 | );↵ | | 12 | s.persist(p2);↵
|
13 | t.commit();↵ | | 13 | t.commit();↵
|
14 | s.close();↵ | | 14 | s.close();↵
|
| | | 15 | ↵
|
15 | s = openSession();↵ | | 16 | s = openSession();↵
|
16 | t = s.beginTransaction();↵ | | 17 | t = s.beginTransaction();↵
|
17 | g = (Group) s.get(Group.class, "developers");↵ | | 18 | p = (↵
|
18 | gavin = (User) g.getUsers().get("gavin");↵ | | 19 | Person) s.createQuery("from Person where name='Gavin'")↵
|
19 | turin = (User) g.getUsers().get("turin");↵ | | 20 | .uniqueResult();↵
|
20 | assertFalse( Hibernate.isInitialized( g.getUsers() ) );↵ | | 21 | //assertFalse( Hibernate.isPropertyInitialized(↵
|
21 | g.getUsers().clear();↵ | | |
|
22 | gavin.getSession().remove("foo"↵ | | 22 | p, "employee") );↵
|
23 | );↵ | | 23 | assertSame( p.getEmployee().getPerson(), p );↵
|
24 | assertTrue( Hibernate.isInitialized( g.getUsers() ) );↵ | | 24 | assertTrue( Hibernate.isInitialized( ↵
|
25 | assertTrue( Hibernate.isInitialized( gavin.getSession() ↵ | | 25 | p.getEmployee().getEmployments() ) );↵
|
| | | 26 | assertEquals( p.getEmployee().getEmployments().size(), 1 );↵
|
| | | 27 | p2 = (Person) s.createQuery("from Person where name='Emmanuel'").uniqueResult();↵
|
26 | ) );↵ | | 28 | assertNull( p2.getEmployee() );↵
|
27 | t.commit();↵ | | 29 | t.commit();↵
|
28 | s.close();↵ | | 30 | s.close();↵
|
|
29 | s = openSession();↵ | | 31 | s = openSession();↵
|
30 | t = s.beginTransaction();↵ | | 32 | t = s.beginTransaction();↵
|
31 | g = (Group) s.get(Group.class, "developers"↵ | | 33 | p = (Person) s.get(Person.class, "Gavin");↵
|
32 | );↵ | | 34 | //assertFalse( Hibernate.isPropertyInitialized(p, "employee") );↵
|
33 | assertTrue( g.getUsers().isEmpty() );↵ | | 35 | assertSame( p.getEmployee().getPerson(), p );↵
|
34 | assertFalse( Hibernate.isInitialized( g.getUsers() ) );↵ | | 36 | assertTrue( Hibernate.isInitialized( ↵
|
35 | gavin = (User) s.get(User.class, "gavin");↵ | | |
|
36 | assertFalse( gavin.getSession().containsKey("foo") );↵ | | |
|
37 | assertFalse( Hibernate.isInitialized( gavin.getSession() ↵ | | 37 | p.getEmployee().getEmployments() ) );↵
|
| | | 38 | assertEquals( p.getEmployee().getEmployments().size(), 1 );↵
|
| | | 39 | p2 = (Person) s.get(Person.class, "Emmanuel");↵
|
38 | ) );↵ | | 40 | assertNull( p2.getEmployee() );↵
|
39 | s.delete(gavin);↵ | | 41 | s.delete(p2);↵
|
40 | s.delete(turin);↵ | | 42 | s.delete(old);↵
|
41 | s.delete(g);↵ | | 43 | s.delete(p);↵
|
42 | t.commit();↵ | | 44 | t.commit();↵
|
43 | s.close();↵ | | 45 | s.close();↵
|
44 | | | 46 |
|