copy child collection to another object

Posted by Bogdan on Stack Overflow See other posts from Stack Overflow or by Bogdan
Published on 2010-06-04T06:16:34Z Indexed on 2010/06/04 6:19 UTC
Read the original article Hit count: 231

Filed under:
|
|
|

Hi everyone,

I have a one-to-many relationship between Part and Params (a "Part" has many "Params).

I'm trying to do something naive like this:

Part sourcePart = em.find(Part.class, partIdSource);
Part destPart = em.find(Part.class, partIdDest);

Collection<Param> paramListSource = sourcePart.getParamList();

destPart.setParamList(paramListSource);

Basically I want to copy all the parameters from sourcePart to destPart. Hopefully the persistence provider will automatically set the right foreign keys in the Param table/entity.

The above code will obviously not work.

Is there any easy way of doing this, or do I have to do create a new collection, then add each Param (creating new Param, setting attributes, etc) ?

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate