Skip to content

Commit

Permalink
HHH-4919 : DefaultMergeEventListener does not call Interceptor.instan…
Browse files Browse the repository at this point in the history
…tiate() for a new persistent entity (Francesco Degrassi)

git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_3@19118 1b8cb986-b30d-0410-93ca-fae66ebed9b2
  • Loading branch information
gbadner committed Mar 26, 2010
1 parent 939e710 commit 19887a4
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 69 deletions.
Expand Up @@ -295,8 +295,7 @@ protected Object mergeTransientEntity(Object entity, String entityName, Serializ
persister.setIdentifier( copyCache.get( entity ), id, source.getEntityMode() );
}
else {
( ( EventCache ) copyCache ).put( entity, persister.instantiate( id, source.getEntityMode() ), 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 );

Expand Down
95 changes: 34 additions & 61 deletions testsuite/src/test/java/org/hibernate/test/criteria/LikeTest.java
Expand Up @@ -42,75 +42,48 @@ public String[] getMappings() {
return new String[]{"criteria/TestObject.hbm.xml"};
}
public void testLike(){
Session session=openSession();
Session session = openSession();
Transaction tx = session.beginTransaction();
TestObject obj = new TestObject();
String uniq = "uniq" + System.currentTimeMillis();

// insert object
try {
Transaction tx = session.beginTransaction();


TestObject obj = new TestObject();
obj.setText("XyZ " + uniq + " blablabla");

session.saveOrUpdate(obj);
session.flush();
tx.commit();
} finally {
session.close();

}
obj.setText( "XyZ " + uniq + " blablabla" );
session.save( obj );
session.flush();
tx.commit();
session.close();
String pattern = "XyZ " + uniq + "%";

// retrieve object - case sensitive - works ok
session = openSession();
try {
List objects = session.createCriteria(TestObject.class)
.add(Restrictions.like("text", pattern))
.list();

assertEquals(1, objects.size());
} finally {
session.close();
}
tx = session.beginTransaction();
List objects = session.createCriteria( TestObject.class ).add(
Restrictions.like( "text", pattern ) ).list();
assertEquals( 1, objects.size() );
session.clear();

// retrieve object - case insensitive - works ok
session = openSession();
try {
List objects = session.createCriteria(TestObject.class)
.add(Restrictions.like("text", pattern).ignoreCase())
.list();

assertEquals(1, objects.size());
} finally {
session.close();
}
objects = session.createCriteria( TestObject.class ).add(
Restrictions.like( "text", pattern ).ignoreCase() ).list();


// retrieve object - case insensitive via custom expression - works ok
session = openSession();
try {
List objects = session.createCriteria(TestObject.class)
.add(StringExpression.stringExpression("text", pattern, true))
.list();

assertEquals(1, objects.size());
} finally {
session.close();
}
assertEquals( 1, objects.size() );
session.clear();
// retrieve object - case insensitive via custom expression - works
// ok
objects = session.createCriteria( TestObject.class ).add(
StringExpression.stringExpression( "text", pattern, true ) )
.list();


// retrieve object - case sensitive via custom expression - not working
session = openSession();
try {
List objects = session.createCriteria(TestObject.class)
.add(StringExpression.stringExpression("text", pattern, false))
.list();

assertEquals(1, objects.size());
} finally {
session.close();
}
assertEquals( 1, objects.size() );
session.clear();

// retrieve object - case sensitive via custom expression - not
// working
objects = session.createCriteria( TestObject.class )
.add(
StringExpression.stringExpression( "text", pattern,
false ) ).list();
assertEquals( 1, objects.size() );
tx.rollback();
session.close();

}
}
Expand Up @@ -4,11 +4,10 @@
import org.hibernate.criterion.LikeExpression;

public class StringExpression extends LikeExpression {
private final static Character ESCAPE_CODE = new Character( '\\' );

protected StringExpression( String property, String value,
boolean ignoreCase ) {
super( property, value, ESCAPE_CODE, ignoreCase );
super( property, value, null, ignoreCase );
}

public static Criterion stringExpression( String propertyName,
Expand Down
Expand Up @@ -5,11 +5,9 @@
<class name="TestObject" table="test">
<id name="id">
<column name="ID" />
<generator class="sequence">
<param name="sequence">test_seq</param>
</generator>
<generator class="native" />
</id>

<property name="text" />
</class>
</hibernate-mapping>
</hibernate-mapping>
@@ -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;
}
}
Expand Up @@ -164,5 +164,48 @@ public void testStatefulIntercept() {
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();
}
}

Expand Up @@ -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();
Expand Down Expand Up @@ -50,4 +51,10 @@ public Calendar getCreated() {
public void setCreated(Calendar created) {
this.created = created;
}
public String getInjectedString() {
return injectedString;
}
public void setInjectedString(String injectedString) {
this.injectedString = injectedString;
}
}

0 comments on commit 19887a4

Please sign in to comment.