that is one indirect way of tackling the issue of running across the culprit of LazyInitializationException when reflecting over proxied objects outside of Session context. The concept of configuring Hibernate to perform eager fetching of relationships is really a bad idea specially when object graphs could go too deep with massive n+1 hierarchies, moreover the 'session-in-view' perhaps may be what you not want as well. .
So in this particular instance with BlazeDS/Flex AMF binary serializations and using Spring + Hibernate for persistence I'd like to share one personal approach.
The verdict: you could do pruned DTO by hand or if dealing with raw hibernate objects then: (may not apply to all cases) but try to stay with lazy (default) as much as possible, when there's a need to walk through an object graph do it programaticaly or use 'fetch join' in hql queries as an alternative, if BlazeDS barks here and there with LazyInitException then below is a reference implementation approach that worked ok (when prototyping), so it'll instruct serializers via a custom bean proxy which registers Serializable type (or register more granular types as needed) to filter hibernate proxied beans | persistent collections when the marshaling happens in detach mode outside session.transactional context. You could take it a bit further to utilize Hibernate.initialize(obj) within current session context as it is explained here (look for Initializing collections and proxies).
Cheers!
package com.martin.orm.io;
import java.io.Serializable;
import flex.messaging.io.BeanProxy;
import flex.messaging.io.PropertyProxyRegistry;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.collection.AbstractPersistentCollection;
public class HbmLazyAmfFilterProxy extends BeanProxy {
private static final long serialVersionUID = 7538263979358789547L;
public HbmLazyAmfFilterProxy() {
super();
PropertyProxyRegistry.getRegistry().register(Serializable.class,this);
}
@Override
public Object getValue(Object o, String p) {
Object val = super.getValue( o , p );
if ( ( val instanceof HibernateProxy) ||
( val instanceof AbstractPersistentCollection ) ) {
val = null;
}
return val;
}
}
Or, a more generic bean utility dto reflective populator could be something like this:
//////////////////////////////////////////////////////////////////////////
public void filter( final T orig , final T dest ) throws Exception {
final PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
final PropertyDescriptor[] origDescriptors =
propertyUtilsBean.getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
final String name = origDescriptors[i].getName();
if ("class".equals(name)) continue;
if ( propertyUtilsBean.isReadable ( orig , name ) &&
propertyUtilsBean.isWriteable( dest , name ) ) {
try {
final Object value =
propertyUtilsBean.getSimpleProperty( orig , name );
if ( ( value instanceof HibernateProxy) ||
( value instanceof AbstractPersistentCollection ) ) {
continue; // filter
}
BeanUtils.copyProperty( dest , name , value );
} catch (final NoSuchMethodException e) {
// deal with it
}
}
}
}





