I'll send you my version: package hibernate;
import java.beans.PropertyDescriptor; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.List; import org.apache.commons.beanutils.PropertyUtils; import org.hibernate.EntityMode; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.metadata.ClassMetadata; import org.hibernate.proxy.HibernateProxyHelper; import java.io.Serializable; import java.sql.Blob; import java.util.*; import org.apache.log4j.Logger; import org.hibernate.proxy.HibernateProxy; public class HibernateCleaner { private static final Logger logger = Logger.getLogger(HibernateCleaner.class); public static Object clean(Session session, Object obj) throws Exception { return (clean(session, obj, new HashMap<Integer, Object>())); } private static Object clean(Session session, Object obj, Map<Integer, Object> visitedObjects) throws Exception { Object newObj; Map.Entry m; Class clazz; Object[] array; Collection collection; Map map; PropertyDescriptor[] descriptors; String property; ClassMetadata clazzMetaData; int index, length, hashCode; if (obj == null) return (null); if ((obj instanceof Boolean) || (obj instanceof Number) || (obj instanceof Character) || (obj instanceof String) || (obj instanceof Blob) || (obj instanceof InputStream)) return (obj); if (obj instanceof Date) return (new Date (((Date) obj).getTime())); if (obj instanceof Calendar) return (((Calendar) obj).clone()); if (obj instanceof Object[]) { array = (Object[]) ((Object[]) obj).clone(); length = array.length; for (index = 0; index < length; index++) array[index] = clean(session, array[index], visitedObjects); return (array); } if (obj instanceof Object[]) { array = (Object[]) ((Object[]) obj).clone(); length = array.length; for (index = 0; index < length; index++) array[index] = clean(session, array[index], visitedObjects); return (array); } if (obj instanceof Collection) { collection = createCollection((Collection) obj); if (Hibernate.isInitialized(obj)) { for (Object member: (Collection) obj) collection.add (clean(session, member, visitedObjects)); } return (collection); } if (obj instanceof Map) { map = createMap((Map) obj); if (Hibernate.isInitialized(obj)) { for (Object member: ((Map)obj).entrySet()) { m = (Map.Entry) member; clean(session, m.getKey(), visitedObjects); clean(session, m.getValue(), visitedObjects); map.put (m.getKey(), m.getValue()); } } return (map); } hashCode = System.identityHashCode(obj); if (visitedObjects.containsKey(hashCode)) return visitedObjects.get(hashCode); if (obj instanceof HibernateProxy) { clazz = HibernateProxyHelper.getClassWithoutInitializingProxy(obj); newObj = clazz.newInstance(); } else newObj = obj.getClass().newInstance(); visitedObjects.put(hashCode, newObj); if (!Hibernate.isInitialized(obj)) { if (session != null) { clazzMetaData = session.getSessionFactory().getClassMetadata(newObj.getClass()); Serializable id = clazzMetaData.getIdentifier(obj, EntityMode.POJO); clazzMetaData.setIdentifier(newObj, id, EntityMode.POJO); } } else { descriptors = PropertyUtils.getPropertyDescriptors(newObj); length = descriptors.length; for (index = 0; index < length; index++) { property = descriptors[index].getName(); if (!property.equals("class")) { try { PropertyUtils.setProperty(newObj, property, clean(session, PropertyUtils.getProperty(obj, property), visitedObjects)); } catch (NoSuchMethodException e) { } } } } return (newObj); } private static Collection createCollection(Collection obj) throws Exception { Collection newObj = null; if (obj instanceof SortedSet) newObj = new TreeSet (); else if (obj instanceof Set) newObj = new HashSet (); else newObj = new ArrayList (); return (newObj); } private static Map createMap(Map obj) throws Exception { Map newObj = null; if (obj instanceof SortedMap) newObj = new TreeMap (); else newObj = new HashMap (); return (newObj); } } Regards, Néstor Boscán On Thu, Jan 27, 2011 at 2:15 AM, Nagin Kothari <naginkoth...@gmail.com>wrote: > > Hi, > Can you point to some link that provide sample code or utility to clean the > Hibernate POJO (Hibernate Cleaner class) as you have mentioned in this mail. > It would greately help me too. > > Thanks in advance, > > Nagin > 2011/1/26 Néstor Boscán <nesto...@gmail.com> > >> This is very tipical in Hibernate applications that have to serialize >> objects through the network be it GWT, Web Services, JSON, etc. Hibernate >> will leave his own POJO in your objects for lazy initialization purposes. >> When GWT or a Web Service tries to serialize the objects and opens the >> hibernate POJO it will throw LazyInitializationException because the >> connection is already closed. What I've done in my applications is to clean >> the POJO of any Hibernate references. This is done with a Hibernate Cleaner >> class. On the web there are a couple of examples of this. >> >> Regards, >> >> Néstor Boscán >> >> On Tue, Jan 25, 2011 at 2:33 PM, Debashish <mr.debash...@gmail.com>wrote: >> >>> Hi >>> >>> I am trying for a sample using GWT and database layer as Hibernate >>> with Oracle on Google Aps server. >>> >>> I have done a sample for Hibernate with JSP as presentation layer, >>> which is working fine. >>> But the same approach is not working with GWT. >>> >>> Here is a verygood link on this topic >>> >>> http://code.google.com/webtoolkit/articles/using_gwt_with_hibernate.html >>> and my approach is almost similar. >>> >>> I have followed the below approach, >>> The RemoteServiceServlet makes a call to Hibernate layer and tries the >>> initializes the SessionFactory as below, >>> sessionFactory = new >>> Configuration().configure().buildSessionFactory(); >>> >>> I have added the jars necessary for the Hibernate layer (and >>> classes12.jar for oracle driver). >>> >>> However while running , it is giving me an error as below, >>> >>> -------------------------------------------------------------------------------------------------------- >>> Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: >>> java.net.Socket is a restricted class. Please see the Google App >>> Engine developer's guide for more details. >>> [ERROR] javax.servlet.ServletContext log: Exception while dispatching >>> incoming RPC call >>> com.google.gwt.user.server.rpc.UnexpectedException: Service method >>> 'public abstract java.lang.Long >>> >>> com.google.musicstore.client.MusicStoreService.saveAccount(com.google.musicstore.client.dto.AccountDTO)' >>> threw an unexpected exception: java.lang.ExceptionInInitializerError >>> at >>> com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java: >>> 378) >>> >>> -------------------------------------------------------------------------------------------------------- >>> I have one through some of the threads which suggested me to download >>> the google apps jar files and try it. I have downladed it. but there >>> is no proper direction on how to make use of which jar file. >>> >>> Have anybody tried the same. Please let me know if anything I am doing >>> wrong. >>> *** Note that I am using Oracle 9.2 as local database. >>> >>> Thanks >>> Dev >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Google Web Toolkit" group. >>> To post to this group, send email to google-web-toolkit@googlegroups.com >>> . >>> To unsubscribe from this group, send email to >>> google-web-toolkit+unsubscr...@googlegroups.com<google-web-toolkit%2bunsubscr...@googlegroups.com> >>> . >>> For more options, visit this group at >>> http://groups.google.com/group/google-web-toolkit?hl=en. >>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Google Web Toolkit" group. >> To post to this group, send email to google-web-toolkit@googlegroups.com. >> To unsubscribe from this group, send email to >> google-web-toolkit+unsubscr...@googlegroups.com<google-web-toolkit%2bunsubscr...@googlegroups.com> >> . >> For more options, visit this group at >> http://groups.google.com/group/google-web-toolkit?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Google Web Toolkit" group. > To post to this group, send email to google-web-toolkit@googlegroups.com. > To unsubscribe from this group, send email to > google-web-toolkit+unsubscr...@googlegroups.com<google-web-toolkit%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/google-web-toolkit?hl=en. > -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-toolkit@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.