You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Matthew Broadhead <ma...@nbmlaw.co.uk.INVALID> on 2018/07/27 11:05:08 UTC

clone object

is there a way to deep clone a JPA object automatically?  i.e. to copy 
all the parameters and children but clear the id and version fields?

i saw that it is possible to do em.detach(object); and then reset the id 
and version but by the time i have done that i may as well have written 
a full copy function.

Re: clone object

Posted by Mark Struberg <st...@yahoo.de.INVALID>.
In practice this is likelymuch more complicated. Where are the boundaries?
If you have a Person which has a relation to a Company, then I assume you do not want to also clone the whole Company, right? ;)
But of course, the n Address rows probably should get cloned?

Do you see what I mean? This is imo all depending on your exact intention and the tables in question.

LieGrue,
strub


> Am 27.07.2018 um 16:08 schrieb Matthew Broadhead <ma...@nbmlaw.co.uk.INVALID>:
> 
> i have a complicated object with a large number of children, each child possibly having their own large number of children.  what would be great in OpenJPA is if you could, say, detach an object and run a command which would null all @Id and @Version fields in the detached Object and all its descendants.  i suppose it has to be done manually though...
> 
> On 27/07/18 15:52, Albert Lee wrote:
>> If the java object implements Serializable or Clonable, you can
>> writeObject/readObject to clone to a new entity.  The new object will be
>> detached and you can reset the id and version as you wish.
>> 
>> 
>> On Fri, Jul 27, 2018 at 8:39 AM, Matthew Broadhead <
>> matthew.broadhead@nbmlaw.co.uk.invalid> wrote:
>> 
>>> i don't think it would clear the id and version fields?  do you mean using
>>> SerializationUtils clone?
>>> https://commons.apache.org/proper/commons-lang/apidocs/org/
>>> apache/commons/lang3/SerializationUtils.html
>>> 
>>> 
>>> On 27/07/18 15:09, Albert Lee wrote:
>>> 
>>>> If the entity is serializable have you try writing it out and read it back
>>>> to a new object?
>>>> 
>>>> On Fri, Jul 27, 2018, 6:05 AM Matthew Broadhead
>>>> <ma...@nbmlaw.co.uk.invalid> wrote:
>>>> 
>>>> is there a way to deep clone a JPA object automatically?  i.e. to copy
>>>>> all the parameters and children but clear the id and version fields?
>>>>> 
>>>>> i saw that it is possible to do em.detach(object); and then reset the id
>>>>> and version but by the time i have done that i may as well have written
>>>>> a full copy function.
>>>>> 
>>>>> 
>> 
> 


Re: clone object

Posted by Matthew Broadhead <ma...@nbmlaw.co.uk.INVALID>.
i have a complicated object with a large number of children, each child 
possibly having their own large number of children.  what would be great 
in OpenJPA is if you could, say, detach an object and run a command 
which would null all @Id and @Version fields in the detached Object and 
all its descendants.  i suppose it has to be done manually though...

On 27/07/18 15:52, Albert Lee wrote:
> If the java object implements Serializable or Clonable, you can
> writeObject/readObject to clone to a new entity.  The new object will be
> detached and you can reset the id and version as you wish.
>
>
> On Fri, Jul 27, 2018 at 8:39 AM, Matthew Broadhead <
> matthew.broadhead@nbmlaw.co.uk.invalid> wrote:
>
>> i don't think it would clear the id and version fields?  do you mean using
>> SerializationUtils clone?
>> https://commons.apache.org/proper/commons-lang/apidocs/org/
>> apache/commons/lang3/SerializationUtils.html
>>
>>
>> On 27/07/18 15:09, Albert Lee wrote:
>>
>>> If the entity is serializable have you try writing it out and read it back
>>> to a new object?
>>>
>>> On Fri, Jul 27, 2018, 6:05 AM Matthew Broadhead
>>> <ma...@nbmlaw.co.uk.invalid> wrote:
>>>
>>> is there a way to deep clone a JPA object automatically?  i.e. to copy
>>>> all the parameters and children but clear the id and version fields?
>>>>
>>>> i saw that it is possible to do em.detach(object); and then reset the id
>>>> and version but by the time i have done that i may as well have written
>>>> a full copy function.
>>>>
>>>>
>


Re: clone object

Posted by Albert Lee <al...@gmail.com>.
If the java object implements Serializable or Clonable, you can
writeObject/readObject to clone to a new entity.  The new object will be
detached and you can reset the id and version as you wish.


On Fri, Jul 27, 2018 at 8:39 AM, Matthew Broadhead <
matthew.broadhead@nbmlaw.co.uk.invalid> wrote:

> i don't think it would clear the id and version fields?  do you mean using
> SerializationUtils clone?
> https://commons.apache.org/proper/commons-lang/apidocs/org/
> apache/commons/lang3/SerializationUtils.html
>
>
> On 27/07/18 15:09, Albert Lee wrote:
>
>> If the entity is serializable have you try writing it out and read it back
>> to a new object?
>>
>> On Fri, Jul 27, 2018, 6:05 AM Matthew Broadhead
>> <ma...@nbmlaw.co.uk.invalid> wrote:
>>
>> is there a way to deep clone a JPA object automatically?  i.e. to copy
>>> all the parameters and children but clear the id and version fields?
>>>
>>> i saw that it is possible to do em.detach(object); and then reset the id
>>> and version but by the time i have done that i may as well have written
>>> a full copy function.
>>>
>>>
>


-- 
Albert Lee.

Re: clone object

Posted by Matthew Broadhead <ma...@nbmlaw.co.uk.INVALID>.
i don't think it would clear the id and version fields?  do you mean 
using SerializationUtils clone?
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/SerializationUtils.html

On 27/07/18 15:09, Albert Lee wrote:
> If the entity is serializable have you try writing it out and read it back
> to a new object?
>
> On Fri, Jul 27, 2018, 6:05 AM Matthew Broadhead
> <ma...@nbmlaw.co.uk.invalid> wrote:
>
>> is there a way to deep clone a JPA object automatically?  i.e. to copy
>> all the parameters and children but clear the id and version fields?
>>
>> i saw that it is possible to do em.detach(object); and then reset the id
>> and version but by the time i have done that i may as well have written
>> a full copy function.
>>


Re: clone object

Posted by Albert Lee <al...@gmail.com>.
If the entity is serializable have you try writing it out and read it back
to a new object?

On Fri, Jul 27, 2018, 6:05 AM Matthew Broadhead
<ma...@nbmlaw.co.uk.invalid> wrote:

> is there a way to deep clone a JPA object automatically?  i.e. to copy
> all the parameters and children but clear the id and version fields?
>
> i saw that it is possible to do em.detach(object); and then reset the id
> and version but by the time i have done that i may as well have written
> a full copy function.
>