[hibernate-commits] Hibernate SVN: r19117 - in core/trunk: testsuite/src/test/java/org/hibernate/test/interceptor and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Mar 26 18:53:04 EDT 2010


Author: gbadner
Date: 2010-03-26 18:53:04 -0400 (Fri, 26 Mar 2010)
New Revision: 19117

Added:
   core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/InstantiateInterceptor.java
Modified:
   core/trunk/core/src/main/java/org/hibernate/event/def/DefaultMergeEventListener.java
   core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/InterceptorTest.java
   core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/User.java
Log:
DefaultMergeEventListener does not call Interceptor.instantiate() for a new persistent entity (Francesco Degrassi)

Modified: core/trunk/core/src/main/java/org/hibernate/event/def/DefaultMergeEventListener.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/event/def/DefaultMergeEventListener.java	2010-03-25 20:32:31 UTC (rev 19116)
+++ core/trunk/core/src/main/java/org/hibernate/event/def/DefaultMergeEventListener.java	2010-03-26 22:53:04 UTC (rev 19117)
@@ -295,8 +295,7 @@
 			persister.setIdentifier( copyCache.get( entity ), id, source );
 		}
 		else {
-			( ( EventCache ) copyCache ).put( entity, persister.instantiate( id, source ), true ); //before cascade!
-			//TODO: should this be Session.instantiate(Persister, ...)?
+			( ( EventCache ) copyCache ).put( entity, source.instantiate( persister, id ), true ); //before cascade!
 		}
 		final Object copy = copyCache.get( entity );
 

Added: core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/InstantiateInterceptor.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/InstantiateInterceptor.java	                        (rev 0)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/InstantiateInterceptor.java	2010-03-26 22:53:04 UTC (rev 19117)
@@ -0,0 +1,54 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ *
+ */
+
+/**
+ * @author Gail Badner
+ */
+package org.hibernate.test.interceptor;
+
+import java.io.Serializable;
+
+import org.hibernate.CallbackException;
+import org.hibernate.EmptyInterceptor;
+import org.hibernate.EntityMode;
+
+public class InstantiateInterceptor extends EmptyInterceptor {
+	private String injectedString;
+
+	public InstantiateInterceptor(String injectedString) {
+		this.injectedString = injectedString;		
+	}
+
+	public Object instantiate(String entityName, EntityMode entityMode, Serializable id) throws CallbackException {
+		if ( ! "org.hibernate.test.interceptor.User".equals( entityName ) ) {
+			return null;
+		}
+		// Simply inject a sample string into new instances
+		User instance = new User();
+		instance.setName( ( String ) id );
+		instance.setInjectedString( injectedString );
+		return instance;
+	}
+}
\ No newline at end of file

Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/InterceptorTest.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/InterceptorTest.java	2010-03-25 20:32:31 UTC (rev 19116)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/InterceptorTest.java	2010-03-26 22:53:04 UTC (rev 19117)
@@ -164,5 +164,48 @@
 		s.close();
 	}
 
+	public void testInitiateIntercept() {
+		final String injectedString = "******";
+		final InstantiateInterceptor initiateInterceptor = new InstantiateInterceptor( injectedString );
+		Session s = openSession( initiateInterceptor );
+
+		Transaction t = s.beginTransaction();
+		User u = new User( "Gavin", "nivag" );
+		s.persist( u );
+		t.commit();
+		s.close();
+
+		assertNull( u.getInjectedString() );
+		u.setPassword( "blah" );
+
+		s = openSession( initiateInterceptor );
+		t = s.beginTransaction();
+
+		User merged = ( User ) s.merge( u );
+		assertEquals( injectedString, merged.getInjectedString() );
+		assertEquals( u.getName(), merged.getName() );
+		assertEquals( u.getPassword(), merged.getPassword() );
+
+		merged.setInjectedString( null );
+
+		User loaded = ( User ) s.load(User.class, merged.getName());
+		// the session-bound instance was not instantiated by the interceptor, load simply returns it
+		assertSame( merged, loaded );
+		assertNull( merged.getInjectedString() );
+
+		// flush the session and evict the merged instance from session to force an actual load
+		s.flush();
+		s.evict( merged );
+
+		User reloaded = ( User ) s.load( User.class, merged.getName() );
+		// Interceptor IS called for instantiating the persistent instance associated to the session when using load
+		assertEquals( injectedString, reloaded.getInjectedString() );
+		assertEquals( u.getName(), reloaded.getName() );
+		assertEquals( u.getPassword(), reloaded.getPassword() );
+
+		s.delete( reloaded );
+		t.commit();
+		s.close();
+	}
 }
 

Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/User.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/User.java	2010-03-25 20:32:31 UTC (rev 19116)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/interceptor/User.java	2010-03-26 22:53:04 UTC (rev 19117)
@@ -11,6 +11,7 @@
 	private Set actions = new HashSet();
 	private Calendar lastUpdated;
 	private Calendar created;
+	private String injectedString;
 	
 	public User(String name, String password) {
 		super();
@@ -50,4 +51,10 @@
 	public void setCreated(Calendar created) {
 		this.created = created;
 	}
+	public String getInjectedString() {
+		return injectedString;
+	}
+	public void setInjectedString(String injectedString) {
+		this.injectedString = injectedString;
+	}
 }



More information about the hibernate-commits mailing list