FileDocCategorySizeDatePackage
OneToOneCacheTest.javaAPI DocHibernate 3.2.52695Tue Dec 12 16:22:26 GMT 2006org.hibernate.test.legacy

OneToOneCacheTest

public class OneToOneCacheTest extends LegacyTestCase
Simple testcase to illustrate HB-992
author
Wolfgang Voelkl, michael

Fields Summary
private Serializable
generatedId
Constructors Summary
public OneToOneCacheTest(String x)

		super( x );
	
Methods Summary
private voidaddObject2()
loads the newly created MainObject and adds a new Object2 to it

one hibernate transaction

		Session session = openSession();
		Transaction tx = session.beginTransaction();

		MainObject mo =
				( MainObject ) session.load( MainObject.class, generatedId );

		Object2 toAdd = new Object2();
		toAdd.setDummy( "test" );

		//toAdd should now be saved by cascade
		mo.setObj2( toAdd );

		tx.commit();
		session.close();
	
private voidcreateMainObject()
creates a new MainObject

one hibernate transaction !

		Session session = openSession();
		Transaction tx = session.beginTransaction();

		MainObject mo = new MainObject();
		mo.setDescription( "Main Test" );

		generatedId = session.save( mo );

		tx.commit();
		session.close();
	
public java.lang.String[]getMappings()

		return new String[] { "legacy/Object2.hbm.xml", "legacy/MainObject.hbm.xml" };
	
private MainObjectreadMainObject()
reads the newly created MainObject and its Object2 if it exists

one hibernate transaction

		Long returnId = null;
		Session session = openSession();
		Transaction tx = session.beginTransaction();

		Serializable id = generatedId;

		MainObject mo = ( MainObject ) session.load( MainObject.class, id );

		tx.commit();
		session.close();

		return mo;
	
public static junit.framework.Testsuite()

		return new FunctionalTestClassTestSuite( OneToOneCacheTest.class );
	
public voidtestOneToOneCache()


		//create a new MainObject
		createMainObject();
		// load the MainObject
		readMainObject();

		//create and add Ojbect2
		addObject2();

		//here the newly created Object2 is written to the database
		//but the MainObject does not know it yet
		MainObject mainObject = readMainObject();

		assertNotNull( mainObject.getObj2() );

		// after evicting, it works.
		getSessions().evict( MainObject.class );

		mainObject = readMainObject();

		assertNotNull( mainObject.getObj2() );