Skip to content

Commit 51beb1d

Browse files
committed
fix for GRAILS-6378 "IllegalArgumentException: object is not an instance of declaring class"
1 parent 7a7b0a3 commit 51beb1d

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/java/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsHibernateUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,21 @@ public static void incrementVersion(Object target) {
324324
}
325325
}
326326

327+
/**
328+
* Ensures the meta class is correct for a given class
329+
*
330+
* @param target The GroovyObject
331+
* @param persistentClass The persistent class
332+
*/
333+
public static void ensureCorrectGroovyMetaClass(Object target, Class persistentClass) {
334+
if(target instanceof GroovyObject) {
335+
GroovyObject go = ((GroovyObject)target);
336+
if(!go.getMetaClass().getTheClass().equals(persistentClass)) {
337+
go.setMetaClass(GroovySystem.getMetaClassRegistry().getMetaClass(persistentClass));
338+
}
339+
}
340+
}
341+
327342
/**
328343
* Unwraps and initializes a HibernateProxy.
329344
* @param proxy The proxy

src/java/org/codehaus/groovy/grails/orm/hibernate/proxy/GroovyAwareJavassistLazyInitializer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*/
1515
package org.codehaus.groovy.grails.orm.hibernate.proxy;
1616

17+
import groovy.lang.GroovyObject;
18+
import groovy.lang.GroovySystem;
19+
1720
import java.io.Serializable;
1821
import java.lang.reflect.InvocationTargetException;
1922
import java.lang.reflect.Method;
@@ -26,6 +29,7 @@
2629
import javassist.util.proxy.ProxyObject;
2730

2831
import org.apache.commons.logging.LogFactory;
32+
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil;
2933
import org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport;
3034
import org.hibernate.HibernateException;
3135
import org.hibernate.engine.SessionImplementor;
@@ -209,6 +213,7 @@ public Object invoke(
209213
}
210214
if (result == INVOKE_IMPLEMENTATION) {
211215
Object target = getImplementation();
216+
GrailsHibernateUtil.ensureCorrectGroovyMetaClass(target,persistentClass);
212217
final Object returnValue;
213218
try {
214219
if (ReflectHelper.isPublic(persistentClass, thisMethod)) {
@@ -240,7 +245,8 @@ public Object invoke(
240245
return proceed.invoke(proxy, args);
241246
}
242247

243-
@Override
248+
249+
@Override
244250
protected Object serializableProxy() {
245251
return new SerializableProxy(
246252
getEntityName(),

src/java/org/codehaus/groovy/grails/orm/hibernate/proxy/HibernateProxyHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.InvocationTargetException;
2020

2121
import org.apache.commons.beanutils.PropertyUtils;
22+
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil;
2223
import org.codehaus.groovy.grails.support.proxy.ProxyHandler;
2324
import org.hibernate.Hibernate;
2425
import org.hibernate.collection.AbstractPersistentCollection;
@@ -79,7 +80,10 @@ public Object unwrapProxy(final HibernateProxy proxy) {
7980
if(lazyInitializer.isUninitialized()) {
8081
lazyInitializer.initialize();
8182
}
82-
return lazyInitializer.getImplementation();
83+
final Object obj = lazyInitializer.getImplementation();
84+
if(obj != null)
85+
GrailsHibernateUtil.ensureCorrectGroovyMetaClass(obj,obj.getClass());
86+
return obj;
8387
}
8488

8589
public HibernateProxy getAssociationProxy(Object obj, String associationName) {

0 commit comments

Comments
 (0)