Does anyone know if there is a framework out there that is able to strip out Hibernate collections from entity classes to make them serializable? I had a look at BeanLib but it only seems to do deep copies of entities while not allowing me to specify implementation mappings for the collection types in my entity classes. BeanLib currently does not work with Hibernate 3.5 and Spring 3 (I did modify the source code and was able to get it working but I’d rather not do this). Gilead seems like an option, but it seems rather invasive. I’d rather use a simple deep copy framework than weave another framework into my entity and DAO classes.
I have tried using XStream with a custom CollectionConverter and MapConverter with success, however… it’s XStream and I don’t really want to be converting my entity to XML and then back from XML in memory. It’s an awful solution.
Will I just have to create my own framework that behaves like XStream does minus all the XML stuff?
My technology stack is: GWT 2.0.4, GWT-Dispatch, Spring 3.0.1 and Hibernate 3.5.2.
My XStream solution:
XStream xstream = new XStream();
xstream.addDefaultImplementation(java.util.ArrayList.class, org.hibernate.collection.PersistentList.class);
xstream.addDefaultImplementation(java.util.HashMap.class, org.hibernate.collection.PersistentMap.class);
xstream.addDefaultImplementation(java.util.HashSet.class, org.hibernate.collection.PersistentSet.class);
xstream.addDefaultImplementation(java.util.ArrayList.class, org.hibernate.collection.PersistentBag.class);
Mapper mapper = xstream.getMapper();
xstream.registerConverter(new HibernateCollectionConverter(mapper));
xstream.registerConverter(new HibernateMapConverter(mapper));
String xml = xstream.toXML(entity);
Entity newEntity = (Entity) xstream.fromXML(xml);