BlazeDS and Hibernate proxied objects
In our project we are using BlazeDS in conjunction with Spring 2 and Hibernate 3. When we send Hibernate objects accross we sometimes get that proxied objects get sent as ‘ClassName$$EnhancerByCGLIB’.
This results in coercion errors on the flex end when the sent objects are being converted into the bound flex objects. To fix this I found the solution on the following page:
Adobe Forums
We use the first solution there:
package com.famvdploeg.util.blazeds; import flex.messaging.io.BeanProxy; import flex.messaging.io.PropertyProxyRegistry; import org.hibernate.proxy.HibernateProxy; /** * This class makes BlazeDS uses the real entity classname instead of the hibernate proxy classname, * this is needed so the entity can be used as a value object (RemoteClass) in flex, * else flex will give a type coercion error because the classname mismatches. * * See the following thread on the BlazeDS forum for more information: * http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=72&catid=667&threadid=1335887&enterthread=y * * @author Steven De Kock */ public class HibernatePropertyProxy extends BeanProxy{ private static final long serialVersionUID = 1612371743382649972L; /** * Registers this class with BlazeDS to handle HibernateProxy instances */ public HibernatePropertyProxy() { super(); PropertyProxyRegistry.getRegistry().register(HibernateProxy.class, this); } /** * Get actual name instead of cglib class name with $$enhancerByCglib in it. */ protected String getClassName(Object o) { if (o instanceof HibernateProxy) { HibernateProxy object = (HibernateProxy) o; return object.getHibernateLazyInitializer().getEntityName(); } else { return super.getClassName(o); } } } |
Although the referenced site states that we should be wary of any lazy initialization exceptions I don’t think that is a problem. This is because the serialization process is taking place behind an opened Hibernate session in our current setup and the proxied objects are still attached to the session. So using this fix works for us.
Oh yeah, don’t forget to load the bean from your spring context. 😉
Put in the following line of xml in your spring context xml.
<!-- Initialize the hibernatePropertyProxy handler so flex gets actual class name instead of $$enhancerbycglib --> <bean id="hibernatePropertyProxy" class="com.famvdploeg.util.blazeds.HibernatePropertyProxy"/> |
Thank you very much!!! I’ve been searching for this all day 🙂
This is so cool !!! thank you so much… Just tried it… I’m accessing my lazy properties withou restrictions… how can it be? does it mean this breaks lazyness? Dunno, but I’m happy 🙂
That was very helpful. Ran into some other scenario but your solution helped me.