2424import org .codehaus .groovy .grails .commons .*;
2525import org .codehaus .groovy .grails .orm .hibernate .GrailsHibernateDomainClass ;
2626import org .codehaus .groovy .grails .orm .hibernate .proxy .GroovyAwareJavassistProxyFactory ;
27+ import org .codehaus .groovy .grails .orm .hibernate .proxy .HibernateProxyHandler ;
28+ import org .codehaus .groovy .grails .support .proxy .ProxyHandler ;
2729import org .hibernate .*;
2830import org .hibernate .criterion .Order ;
2931import org .hibernate .engine .EntityEntry ;
@@ -71,6 +73,7 @@ public class GrailsHibernateUtil {
7173 public static final String CONFIG_PROPERTY_CACHE_QUERIES ="grails.hibernate.cache.queries" ;
7274 public static final Class [] EMPTY_CLASS_ARRAY =new Class [0 ];
7375
76+ private static HibernateProxyHandler proxyHandler = new HibernateProxyHandler ();
7477
7578 public static void configureHibernateDomainClasses (SessionFactory sessionFactory , GrailsApplication application ) {
7679 Map hibernateDomainClassMap = new HashMap ();
@@ -316,11 +319,7 @@ public static void incrementVersion(Object target) {
316319 * @param proxy The proxy
317320 */
318321 public static Object unwrapProxy (HibernateProxy proxy ) {
319- LazyInitializer lazyInitializer = proxy .getHibernateLazyInitializer ();
320- if (lazyInitializer .isUninitialized ()) {
321- lazyInitializer .initialize ();
322- }
323- return lazyInitializer .getImplementation ();
322+ return proxyHandler .unwrapProxy (proxy );
324323 }
325324
326325
@@ -332,20 +331,7 @@ public static Object unwrapProxy(HibernateProxy proxy) {
332331 * @return A proxy
333332 */
334333 public static HibernateProxy getAssociationProxy (Object obj , String associationName ) {
335- try {
336- Object proxy = PropertyUtils .getProperty (obj , associationName );
337- if (proxy instanceof HibernateProxy ) return (HibernateProxy ) proxy ;
338- else return null ;
339- }
340- catch (IllegalAccessException e ) {
341- return null ;
342- }
343- catch (InvocationTargetException e ) {
344- return null ;
345- }
346- catch (NoSuchMethodException e ) {
347- return null ;
348- }
334+ return proxyHandler .getAssociationProxy (obj , associationName );
349335 }
350336
351337 /**
@@ -356,19 +342,7 @@ public static HibernateProxy getAssociationProxy(Object obj, String associationN
356342 * @return True if is initialized
357343 */
358344 public static boolean isInitialized (Object obj , String associationName ) {
359- try {
360- Object proxy = PropertyUtils .getProperty (obj , associationName );
361- return Hibernate .isInitialized (proxy );
362- }
363- catch (IllegalAccessException e ) {
364- return false ;
365- }
366- catch (InvocationTargetException e ) {
367- return false ;
368- }
369- catch (NoSuchMethodException e ) {
370- return false ;
371- }
345+ return proxyHandler .isInitialized (obj , associationName );
372346 }
373347
374348 public static boolean isCacheQueriesByDefault () {
@@ -413,11 +387,6 @@ public static GroovyAwareJavassistProxyFactory buildProxyFactory(PersistentClass
413387 }
414388
415389 public static Object unwrapIfProxy (Object instance ) {
416- if (instance instanceof HibernateProxy ) {
417- return unwrapProxy ((HibernateProxy )instance );
418- }
419- else {
420- return instance ;
421- }
390+ return proxyHandler .unwrapIfProxy (instance );
422391 }
423392}
0 commit comments