简体   繁体   English

具有关系的EJB实体bean的深层复制

[英]Deep copying of EJB Entity beans with relations

What I have is an entity bean eg Entity (EJB 3) that keeps same type children in an ArrayList<Entity> , his parent <Entity> and a relation to another entity <Users> . 我所拥有的是一个实体Bean,例如Entity (EJB 3),它在ArrayList<Entity>中保留相同类型的子代,其父<Entity>以及与另一个实体<Users>的关系。 Users can own many Entities, and vice-versa (many to many). 用户可以拥有许多实体,反之亦然(很多对许多实体)。

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. 我想这样做是重写Entity.clone()或者有一个新的方法),以深拷贝Entity与克隆一起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 (一个新的实体即是),然后用克隆填充children foreach循环内的实体。

But this gives me a concurrent modification exception and I end up with just a clone of the initial Entity bean without its children . 但这给了我一个并发的修改异常,最后我得到的只是初始Entity Bean的一个克隆,没有它的children

My question is: 我的问题是:

Is what I want to do feasible at all or should I manage deep copying from eg a Facade? 我想做的事情是否完全可行,还是应该管理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. 如果可行,请您指导我阅读一些内容或给我一些提示,因为到目前为止,我是通过立面进行克隆的,这已成为我应用程序的主要负担。

Thanks in Advance!! 提前致谢!!

pataroulis pataroulis

Try using (from commons-lang ) 尝试使用(来自commons-lang

YourEntity clone = SerializationUtils.clone(entity);

You would have to make your entities Serializable (which you might not necessarily want, though). 您将不得不使您的实体可Serializable (尽管您可能不一定想要)。 Also this should be done while the EntityManager is still open, otherwise you'd get lazy initialization exception. 同样,应该在EntityManager仍处于打开状态时执行此操作,否则会出现惰性初始化异常。

You have to create a new List, otherwise you are adding to the same List you are iterating over, hence the concurrent modification exception. 您必须创建一个新的List,否则您将添加到要迭代的同一List,从而导致并发修改异常。

ie

Entity clone = super.clone();
clone.setChildren(new ArrayList());
for (Child child : this.getChildren()) {
  clone.addChild(child.clone());
}
return clone;

If you are using EclipseLink your could also use the copy() API on the EclipseLink JpaEntityManager. 如果您使用的是EclipseLink,则还可以在EclipseLink JpaEntityManager上使用copy()API。 You can pass a CopyGroup that specifies how deep to make the copy, and if the Id should be reset. 您可以传递一个CopyGroup,它指定制作副本的深度以及是否应重置ID。

You have to deal with several problems if oyur entities are not detached. 如果您的实体未分离,则必须处理几个问题。 Also you have to clone or serilize your entities outside of transaction scoper otherwithe you'll Get DetachedEntityPassedToPersistException(). 另外,您还必须在事务作用域之外克隆或序列化实体,否则将获得DetachedEntityPassedToPersistException()。 Here is more elaborated answer: . 这里是更详尽的答案:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM