clone object

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

clone object

Matthew Broadhead-2
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.
Reply | Threaded
Open this post in threaded view
|

Re: clone object

allee8285
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
<[hidden email]> 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.
>
Reply | Threaded
Open this post in threaded view
|

Re: clone object

Matthew Broadhead-2
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
> <[hidden email]> 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.
>>

Reply | Threaded
Open this post in threaded view
|

Re: clone object

allee8285
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 <
[hidden email]> 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
>> <[hidden email]> 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.
Reply | Threaded
Open this post in threaded view
|

Re: clone object

Matthew Broadhead-2
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 <
> [hidden email]> 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
>>> <[hidden email]> 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.
>>>>
>>>>
>

Reply | Threaded
Open this post in threaded view
|

Re: clone object

Mark Struberg-3
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 <[hidden email]>:
>
> 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 <
>> [hidden email]> 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
>>>> <[hidden email]> 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.
>>>>>
>>>>>
>>
>