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 = xstr开发者_JAVA技巧eam.getMapper();
xstream.registerConverter(new HibernateCollectionConverter(mapper));
xstream.registerConverter(new HibernateMapConverter(mapper));
String xml = xstream.toXML(entity);
Entity newEntity = (Entity) xstream.fromXML(xml);
Dozer works well for this. Simply map the bean instance to a copy of itself.
obj = dozerBeanMapper.map(obj, obj.getClass());
In mapping the instance to a new instance, Dozer ignores whatever specific runtime implementation is used for the collections and instead uses the standard implementations or whatever your classes default to.
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.
I'm curious, why does it matter what implementation is used for your collection types? As a best practice it's best for your persistent classes to refer to List
, Set
, etc., the actual implementations shouldn't matter to anyone who consumes these classes - they just care about the data.
精彩评论