Hibernate和可序列化的实体 [英] Hibernate and Serializable Entities

查看:100
本文介绍了Hibernate和可序列化的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道是否有一个框架可以从实体类中去掉Hibernate集合以使它们可序列化?我看了看BeanLib,但它似乎只做了实体的深层副本,而不允许我为实体类中的集合类型指定实现映射。 BeanLib目前不支持Hibernate 3.5和Spring 3(我修改了源代码并能够使其工作,但我宁愿不这样做)。基列似乎是一种选择,但它似乎相当侵略性。我宁愿使用一个简单的深层复制框架,而不是将另一个框架编织到我的实体和DAO类中。



我曾尝试将XStream与自定义的CollectionConverter和MapConverter一起使用,然而......这是XStream,我并不想将我的实体转换为XML,然后从内存中的XML转回。这是一个非常糟糕的解决方案。



我是否需要创建自己的框架,其行为与XStream相同?减去所有XML内容?



我的技术堆栈是:GWT 2.0.4,GWT-Dispatch,Spring 3.0.1和Hibernate 3.5.2。

我的XStream解决方案: p>

  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(新的HibernateMapConverter(mapper));

String xml = xstream.toXML(entity);
Entity newEntity =(Entity)xstream.fromXML(xml);


解决方案

推土机适用于此。简单地将bean实例映射到自身的副本。

  obj = dozerBeanMapper.map(obj,obj.getClass()); 

在将实例映射到新实例时,Dozer会忽略用于集合的任何特定运行时实现,而是使用标准实现或任何类默认的​​实现。


我看了一下BeanLib,但它似乎只是做实体的深层副本而不允许我为实体类中的集合类型指定实现映射。


我很好奇,为什么它关系到什么实现是用于你的收藏类型?作为一个最佳实践,最好持久化类引用 List Set 等,实际的实现应该对任何使用这些课程的人都无关紧要 - 他们只关心数据。

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);

解决方案

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.

这篇关于Hibernate和可序列化的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆