Session s = openSession();
Transaction t = s.beginTransaction();
Person p = new Person("Gavin");
Person p2 = new Person("Emmanuel");
Employee e = new Employee(p);
new Employment(e, "JBoss");
Employment old = new Employment(e, "IFA");
old.setEndDate( new Date() );
s.persist(p);
s.persist(p2);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
p = (Person) s.createQuery("from Person where name='Gavin'").uniqueResult();
//assertFalse( Hibernate.isPropertyInitialized(p, "employee") );
assertSame( p.getEmployee().getPerson(), p );
assertTrue( Hibernate.isInitialized( p.getEmployee().getEmployments() ) );
assertEquals( p.getEmployee().getEmployments().size(), 1 );
p2 = (Person) s.createQuery("from Person where name='Emmanuel'").uniqueResult();
assertNull( p2.getEmployee() );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
p = (Person) s.get(Person.class, "Gavin");
//assertFalse( Hibernate.isPropertyInitialized(p, "employee") );
assertSame( p.getEmployee().getPerson(), p );
assertTrue( Hibernate.isInitialized( p.getEmployee().getEmployments() ) );
assertEquals( p.getEmployee().getEmployments().size(), 1 );
p2 = (Person) s.get(Person.class, "Emmanuel");
assertNull( p2.getEmployee() );
s.delete(p2);
s.delete(old);
s.delete(p);
t.commit();
s.close();