[hibernate-commits] Hibernate SVN: r11334 - branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Mar 22 13:36:43 EDT 2007


Author: steve.ebersole at jboss.com
Date: 2007-03-22 13:36:43 -0400 (Thu, 22 Mar 2007)
New Revision: 11334

Modified:
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java
Log:
HHH-2277 : key-many-to-one (bidir + eager) : test

Modified: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java	2007-03-22 17:35:22 UTC (rev 11333)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java	2007-03-22 17:36:43 UTC (rev 11334)
@@ -3,6 +3,10 @@
 import junit.framework.Test;
 
 import org.hibernate.Session;
+import org.hibernate.HibernateException;
+import org.hibernate.event.def.DefaultLoadEventListener;
+import org.hibernate.event.LoadEvent;
+import org.hibernate.event.LoadEventListener;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.Environment;
 import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
@@ -12,6 +16,7 @@
  * @author Steve Ebersole
  */
 public class EagerKeyManyToOneTest extends TestCase {
+
 	public EagerKeyManyToOneTest(String name) {
 		super( name );
 	}
@@ -27,6 +32,12 @@
 	protected void configure(Configuration cfg) {
 		super.configure( cfg );
 		cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
+		LoadEventListener[] baseListeners = cfg.getEventListeners().getLoadEventListeners();
+		int baseLength = baseListeners.length;
+		LoadEventListener[] expandedListeners = new LoadEventListener[ baseLength + 1 ];
+		expandedListeners[ 0 ] = new CustomLoadListener();
+		System.arraycopy( baseListeners, 0, expandedListeners, 1, baseLength );
+		cfg.getEventListeners().setLoadEventListeners( expandedListeners );
 	}
 
 	public void testSaveCascadedToKeyManyToOne() {
@@ -62,6 +73,8 @@
 // essentially we have a bidirectional association where one side of the
 // association is actually part of a composite PK
 //
+// See #testLoadEntityWithEagerFetchingToKeyManyToOneReferenceBackToSelfFailureExpected() below...
+//
 // The way these are mapped causes the problem because both sides
 // are defined as eager which leads to the infinite loop; if only
 // one side is marked as eager, then all is ok...
@@ -89,4 +102,56 @@
 		s.getTransaction().commit();
 		s.close();
 	}
+
+	public void testLoadEntityWithEagerFetchingToKeyManyToOneReferenceBackToSelfFailureExpected() {
+		// long winded method name to say that this is a test specifically for HHH-2277 ;)
+		// essentially we have a bidirectional association where one side of the
+		// association is actually part of a composite PK.
+		//
+		// The way these are mapped causes the problem because both sides
+		// are defined as eager which leads to the infinite loop; if only
+		// one side is marked as eager, then all is ok.  In other words the
+		// problem arises when both pieces of instance data are coming from
+		// the same result set.  This is because no "entry" can be placed
+		// into the persistence context for the association with the
+		// composite key because we are in the process of trying to build
+		// the composite-id instance
+		Session s = openSession();
+		s.beginTransaction();
+		Customer cust = new Customer( "Acme, Inc." );
+		Order order = new Order( new Order.Id( cust, 1 ) );
+		cust.getOrders().add( order );
+		s.save( cust );
+		s.getTransaction().commit();
+		s.close();
+
+		s = openSession();
+		s.beginTransaction();
+		try {
+			cust = ( Customer ) s.get( Customer.class, cust.getId() );
+		}
+		catch( OverflowCondition overflow ) {
+			fail( "get()/load() caused overflow condition" );
+		}
+		s.delete( cust );
+		s.getTransaction().commit();
+		s.close();
+	}
+
+	private static class OverflowCondition extends RuntimeException {
+	}
+
+	private static class CustomLoadListener extends DefaultLoadEventListener {
+		private int internalLoadCount = 0;
+		public void onLoad(LoadEvent event, LoadType loadType) throws HibernateException {
+			if ( LoadEventListener.INTERNAL_LOAD_EAGER.getName().equals( loadType.getName() ) ) {
+				internalLoadCount++;
+				if ( internalLoadCount > 10 ) {
+					throw new OverflowCondition();
+				}
+			}
+			super.onLoad( event, loadType );
+			internalLoadCount--;
+		}
+	}
 }




More information about the hibernate-commits mailing list