具有关系的EJB实体bean的深层复制 [英] Deep copying of EJB Entity beans with relations
问题描述
我拥有的是一个实体bean,例如Entity
(EJB 3),它在ArrayList<Entity>
中保留相同类型的子代,并保留其父代<Entity>
和与另一个实体<Users>
的关系.用户可以拥有多个实体,反之亦然(很多).
What I have is an entity bean e.g. Entity
(EJB 3) that keeps same type children in an ArrayList<Entity>
, his parent <Entity>
and a relation to another entity <Users>
. Users can own many Entities, and vice-versa (many to many).
我想做的是覆盖Entity.clone()
(或有一个新方法)来深度复制Entity
和children
的克隆,该克隆属于同一父级,并分配给已经存在的用户.
What I would like to do is override Entity.clone()
(or have a new method) to deep-copy Entity
along with clones of children
, belonging to the same parent and being assigned to the already existing users.
我已经设置了clone方法来创建Entity
的克隆(是一个新的Entity),然后在foreach循环中用children
实体的克隆填充它.
I have set up the clone method to create a clone of the Entity
(a new Entity that is), and then fill it with clones of the children
entities within a foreach loop.
但是这给了我一个并发的修改异常,最后我得到了一个初始Entity
bean的克隆,而没有它的children
.
But this gives me a concurrent modification exception and I end up with just a clone of the initial Entity
bean without its children
.
我的问题是:
这是我想做的完全可行的事情,还是应该管理例如外墙? 如果可行,请您指导我阅读一些内容或给我一些提示,因为到目前为止,我是通过立面进行克隆的,这已经成为我应用程序中的主要负担.
Is what I want to do feasible at all or should I manage deep copying from e.g. a Facade? If it is feasible, could you direct me please to something to read or give me a couple of hints, because up to now I do the cloning via a facade and it has become a major burden in my application.
先谢谢了!
pataroulis
pataroulis
推荐答案
尝试使用(来自 commons-lang )
YourEntity clone = SerializationUtils.clone(entity);
您必须将您的实体设置为Serializable
(尽管您不一定需要).另外,应该在EntityManager
仍处于打开状态时执行此操作,否则会出现惰性初始化异常.
You would have to make your entities Serializable
(which you might not necessarily want, though). Also this should be done while the EntityManager
is still open, otherwise you'd get lazy initialization exception.
这篇关于具有关系的EJB实体bean的深层复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!