FileDocCategorySizeDatePackage
LazyOneToOneTest.javaAPI DocHibernate 3.2.52578Tue Dec 12 16:22:26 GMT 2006org.hibernate.test.lazyonetoone

LazyOneToOneTest

public class LazyOneToOneTest extends org.hibernate.junit.functional.FunctionalTestCase
author
Gavin King

Fields Summary
Constructors Summary
public LazyOneToOneTest(String str)

		super(str);
	
Methods Summary
public voidconfigure(org.hibernate.cfg.Configuration cfg)

		cfg.setProperty(Environment.MAX_FETCH_DEPTH, "2");
		cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
	
public java.lang.String[]getMappings()

		return new String[] { "lazyonetoone/Person.hbm.xml" };
	
public static booleanisRunnable()

		return FieldInterceptionHelper.isInstrumented( new Person() );
	
public static junit.framework.Testsuite()

		return new FunctionalTestClassTestSuite( LazyOneToOneTest.class );
	
public voidtestLazy()

		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();