DefaultMergeEventListener does not call Interceptor.instantiate() for a new persistent entity (Francesco Degrassi)
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19117 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
d9cbeeaa04
commit
b185946c81
|
@ -295,8 +295,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
|
|||
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 );
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -164,5 +164,48 @@ public class InterceptorTest extends FunctionalTestCase {
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ public class User {
|
|||
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 class User {
|
|||
public void setCreated(Calendar created) {
|
||||
this.created = created;
|
||||
}
|
||||
public String getInjectedString() {
|
||||
return injectedString;
|
||||
}
|
||||
public void setInjectedString(String injectedString) {
|
||||
this.injectedString = injectedString;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue