You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Ognjen Blagojevic <og...@gmail.com> on 2011/01/14 15:12:24 UTC

How to persist duplicate of an entity?

Hi,

Is there an easy way to create duplicate of an entity?

I want to read entity from the DB, and then persist (almost) identical 
copy. Every field should be the same, except primary key.

I know I could probably use BeanUtils.copyProperties, but is there 
OpenJPA preffered way to do the duplication?

Regards,
Ognjen

Re: How to persist duplicate of an entity?

Posted by Rick Curtis <cu...@gmail.com>.
Henno -
> I'm not sure though how this relates to the duplicate entity problem?
After reading through the chain of notes, really it doesn't.

> I guess the pcVersionInit is a useful field for normal operation and
should still be serialized?
It was a conscious decision to not serialize pcVersionInit as we didn't want
to break existing behavior (serialize an enhanced Entity and deserialize it
into an unenhanced Entity).

Thanks,
Rick

On Mon, Feb 21, 2011 at 9:25 AM, Henno Vermeulen <he...@huizemolenaar.nl>wrote:

> Yes I can and I shall use this for any further bug reports I may do.
> I'm not sure though how this relates to the duplicate entity problem? I
> guess the pcVersionInit is a useful field for normal operation and should
> still be serialized?
>
> I already verified that the problem that OpenJPA does not recognize an
> entity that is duplicated with serialization (and id and version set to
> null) as a new one
> is solved by using reflection to set all "pcVersionInit" fields in the
> inheritance hierarchy to false. (I had a mapped superclass which also had
> this field).
>
> Henno
>
> -----Oorspronkelijk bericht-----
> Van: Rick Curtis [mailto:curtisr7@gmail.com]
> Verzonden: maandag 21 februari 2011 16:14
> Aan: users@openjpa.apache.org
> Onderwerp: Re: How to persist duplicate of an entity?
>
> Henno -
>
> Can you give 2.1.0 a try?
>
> Thanks,
> Rick
>
> On Mon, Feb 21, 2011 at 8:51 AM, Henno Vermeulen <henno@huizemolenaar.nl
> >wrote:
>
> > I am using version 2.0.0
> >
> >  (downloaded through Maven
> > ...
> >                                        <dependency>
> >
> >  <groupId>org.apache.openjpa</groupId>
> >
> >  <artifactId>openjpa-all</artifactId>
> >                                                <version>2.0.0</version>
> >                                        </dependency>
> > ...
> >
> > Regards,
> > Henno
> >
> >
> > -----Oorspronkelijk bericht-----
> > Van: Rick Curtis [mailto:curtisr7@gmail.com]
> > Verzonden: maandag 21 februari 2011 15:49
> > Aan: users@openjpa.apache.org
> > Onderwerp: Re: How to persist duplicate of an entity?
> >
> > Henno --
> >
> > What version of openjpa are you using?
> >
> > Thanks,
> > Rick
> >
>

RE: How to persist duplicate of an entity?

Posted by Henno Vermeulen <he...@huizemolenaar.nl>.
Yes I can and I shall use this for any further bug reports I may do.
I'm not sure though how this relates to the duplicate entity problem? I guess the pcVersionInit is a useful field for normal operation and should still be serialized?

I already verified that the problem that OpenJPA does not recognize an entity that is duplicated with serialization (and id and version set to null) as a new one 
is solved by using reflection to set all "pcVersionInit" fields in the inheritance hierarchy to false. (I had a mapped superclass which also had this field).

Henno

-----Oorspronkelijk bericht-----
Van: Rick Curtis [mailto:curtisr7@gmail.com] 
Verzonden: maandag 21 februari 2011 16:14
Aan: users@openjpa.apache.org
Onderwerp: Re: How to persist duplicate of an entity?

Henno -

Can you give 2.1.0 a try?

Thanks,
Rick

On Mon, Feb 21, 2011 at 8:51 AM, Henno Vermeulen <he...@huizemolenaar.nl>wrote:

> I am using version 2.0.0
>
>  (downloaded through Maven
> ...
>                                        <dependency>
>
>  <groupId>org.apache.openjpa</groupId>
>
>  <artifactId>openjpa-all</artifactId>
>                                                <version>2.0.0</version>
>                                        </dependency>
> ...
>
> Regards,
> Henno
>
>
> -----Oorspronkelijk bericht-----
> Van: Rick Curtis [mailto:curtisr7@gmail.com]
> Verzonden: maandag 21 februari 2011 15:49
> Aan: users@openjpa.apache.org
> Onderwerp: Re: How to persist duplicate of an entity?
>
> Henno --
>
> What version of openjpa are you using?
>
> Thanks,
> Rick
>

Re: How to persist duplicate of an entity?

Posted by Rick Curtis <cu...@gmail.com>.
Henno -

Can you give 2.1.0 a try?

Thanks,
Rick

On Mon, Feb 21, 2011 at 8:51 AM, Henno Vermeulen <he...@huizemolenaar.nl>wrote:

> I am using version 2.0.0
>
>  (downloaded through Maven
> ...
>                                        <dependency>
>
>  <groupId>org.apache.openjpa</groupId>
>
>  <artifactId>openjpa-all</artifactId>
>                                                <version>2.0.0</version>
>                                        </dependency>
> ...
>
> Regards,
> Henno
>
>
> -----Oorspronkelijk bericht-----
> Van: Rick Curtis [mailto:curtisr7@gmail.com]
> Verzonden: maandag 21 februari 2011 15:49
> Aan: users@openjpa.apache.org
> Onderwerp: Re: How to persist duplicate of an entity?
>
> Henno --
>
> What version of openjpa are you using?
>
> Thanks,
> Rick
>

RE: How to persist duplicate of an entity?

Posted by Henno Vermeulen <he...@huizemolenaar.nl>.
I am using version 2.0.0

 (downloaded through Maven
...
					<dependency>
						<groupId>org.apache.openjpa</groupId>
						<artifactId>openjpa-all</artifactId>
						<version>2.0.0</version>
					</dependency>
...

Regards,
Henno


-----Oorspronkelijk bericht-----
Van: Rick Curtis [mailto:curtisr7@gmail.com] 
Verzonden: maandag 21 februari 2011 15:49
Aan: users@openjpa.apache.org
Onderwerp: Re: How to persist duplicate of an entity?

Henno --

What version of openjpa are you using?

Thanks,
Rick

Re: How to persist duplicate of an entity?

Posted by Rick Curtis <cu...@gmail.com>.
Henno --

What version of openjpa are you using?

Thanks,
Rick

RE: How to persist duplicate of an entity?

Posted by Henno Vermeulen <he...@huizemolenaar.nl>.
Sorry for replying to an old topic, but you are right. At the moment I also want to copy entities and I am running into the same problem with my serialize deserialize trick, even after setting id and version to null OpenJPA still thinks it is an existing entity, even though I don't use a detached state field and no detached state manager; OpenJPA still adds the pcVersionInit field and sets it to true.

I guess a copy constructor is the safest bet, or otherwise set it to false with Reflection which can get a bit tricky (I had multiple pcVersionInit fields in one entity, probably in superclasses or something).

Henno


-----Oorspronkelijk bericht-----
Van: Mark Struberg [mailto:struberg@yahoo.de] 
Verzonden: vrijdag 14 januari 2011 18:15
Aan: users@openjpa.apache.org
Onderwerp: Re: How to persist duplicate of an entity?

btw, I found the default difference between a 'normally' detached entity and a serialisation/deserialisation detached entity pretty heavy.

This introduced several errors which were pretty hard to explain to an average-joe developer. An application by default might behave completely different if you run it on a local tomcat or in a cluster environment (with sessions getting replicated via memcached all the times). Another example would be using the JSF @ViewScoped (JSF2 ViewMap must get serialized with every request) vs e.g. @RequestScoped.

Because of that I'm currently in the process of switching to 
openjpa.DetachState=loaded(DetachedStateField=true)
which hopefully will remove this obscurities.
As far as I understand, this will also restore the _pcVersionInit and others, isn't?

LieGrue,
strub


--- On Fri, 1/14/11, Rick Curtis <cu...@gmail.com> wrote:

> From: Rick Curtis <cu...@gmail.com>
> Subject: Re: How to persist duplicate of an entity?
> To: users@openjpa.apache.org
> Date: Friday, January 14, 2011, 5:03 PM
> > OpenJPA considers the entity a
> detached object even if you reset the Id
> and version field.
> In the absence of some sort of StateManager (Impl or
> Detached) we use the
> version field and another enhancer added field to detect
> whether an Entity
> is new or detached.
> 
> > Might be a bit too stringent and the error message is
> misleading (read
> wrong).
> Misleading, probably.
> 
> The problem is that we key off the fact that the version
> field was set. @See
> _pcVersionInit in an enhanced Entity. This field is used to
> differentiate
> between a uninitialized primitive and a primitive that was
> actually set to
> the default value (ie: setting an int version to zero). One
> way to get
> around this would be to serialize/deserialize an Entity
> after setting the
> version field. Or you could use reflection to set
> _pcVersionInit to
> false.... but neither options are good ones.
> 
> This is a use case is one that wasn't considered when this
> code was written.
> 
> 
> Thanks,
> Rick..
> 
> On Fri, Jan 14, 2011 at 10:47 AM, Mark Struberg <st...@yahoo.de>
> wrote:
> 
> > I think this is perfectly ok with managed entities. It
> might of course work
> > with detached ones. Just remember that JPA is a ORM
> mapper. Thus a primary
> > key usually must not get changed.
> >
> > LieGrue,
> > strub
> >
> > --- On Fri, 1/14/11, Kevin Sutter <kw...@gmail.com>
> wrote:
> >
> > > From: Kevin Sutter <kw...@gmail.com>
> > > Subject: Re: How to persist duplicate of an
> entity?
> > > To: users@openjpa.apache.org
> > > Date: Friday, January 14, 2011, 4:20 PM
> > > You are right, Jerry...  It
> > > looks like some recent changes to detect version
> > > field updates are preventing this type of
> processing.
> > > Even though the
> > > exception text makes it sound like we should be
> able to get
> > > around it...
> > >
> > > <openjpa-2.2.0-SNAPSHOT-r422266:1057901M
> nonfatal store
> > > error>
> > >
> org.apache.openjpa.persistence.EntityExistsException:
> > > Attempt to persist
> > > detached object
> "simple.model.Person@105a105a".  If
> > > this is a new instance,
> > > make sure any version and/or auto-generated
> primary key
> > > fields are
> > > null/default when persisting.
> > > FailedObject: simple.model.Person@105a105a
> > >
> > > I tried with and without a version field. 
> And, I
> > > tried with both primitive
> > > int and Integer version fields.  No matter
> what I
> > > tried, I hit this
> > > exception either on the first or second commit of
> the
> > > Person instance.  Even
> > > though I am not using auto-generated keys and my
> version
> > > field is either
> > > nulled out or zeroed out.
> > >
> > > Maybe this check is too stringent?
> > >
> > > Anyway, it sounds like this short cut won't work
> for
> > > Ognjen.  Thanks for
> > > keeping me honest.
> > >
> > > Kevin
> > >
> > >
> > > On Fri, Jan 14, 2011 at 9:25 AM, No1UNo <je...@jerrycarter.org>
> > > wrote:
> > >
> > > >
> > > > In a recent application, I was forced to
> resort to a
> > > copy constructor to
> > > > explicitly make a copy of the object. 
> I tried to
> > > reuse and re-persist an
> > > > entity after making a few changes, but any
> attempt to
> > > change the value of
> > > > the @Id was blocked by OpenJPA.
> > > >
> > > > On Jan 14, 2011, at 10:19 AM, Kevin Sutter
> [via
> > > OpenJPA] wrote:
> > > >
> > > > > There are so many variables that can
> come into
> > > play with a scenario like
> > > > > this...
> > > > >
> > > > > If you are only looking to populate the
> database
> > > and you don't care about
> > > > > the actual Entity objects, then you
> could just
> > > populate a single Entity
> > > > > instance.  In a loop, persist and
> commit
> > > this entity, update the key and
> > > > > repeat.  If you are using a
> Version field,
> > > then you'll also have to reset
> > > > > that each time.
> > > > >
> > > > > It all depends on the end game... 
> :-)
> > > > >
> > > > > Kevin
> > > > >
> > > > > On Fri, Jan 14, 2011 at 8:52 AM, Henno
> Vermeulen
> > > <[hidden email]>wrote:
> > > > >
> > > > > > The easiest way that I found to
> create a
> > > deep clone of an object graph
> > > > is
> > > > > > to use apache commons
> collections.
> > > > > >
> > >
> SerializationUtils.deserialize(SerializationUtils.serialize(object)).
> > > > > >
> > > > > > Can of course also be used for a
> simple
> > > entity without relations. In
> > > > anyway
> > > > > > you have to watch out that you
> still set all
> > > primary keys to null. (May
> > > > also
> > > > > > not be most performant way.)
> > > > > >
> > > > > > Regards,
> > > > > > Henno Vermeulen
> > > > > > Huize Molenaar
> > > > > >
> > > > > > -----Oorspronkelijk bericht-----
> > > > > > Van: Ognjen Blagojevic
> [mailto:[hidden
> > > email]]
> > > > > > Verzonden: vrijdag 14 januari 2011
> 15:12
> > > > > > Aan: [hidden email]
> > > > > > Onderwerp: How to persist
> duplicate of an
> > > entity?
> > > > > >
> > > > > > Hi,
> > > > > >
> > > > > > Is there an easy way to create
> duplicate of
> > > an entity?
> > > > > >
> > > > > > I want to read entity from the DB,
> and then
> > > persist (almost) identical
> > > > > > copy. Every field should be the
> same, except
> > > primary key.
> > > > > >
> > > > > > I know I could probably use
> > > BeanUtils.copyProperties, but is there
> > > > > > OpenJPA preffered way to do the
> > > duplication?
> > > > > >
> > > > > > Regards,
> > > > > > Ognjen
> >
> >
> >
> >
> 


      


Re: How to persist duplicate of an entity?

Posted by Rick Curtis <cu...@gmail.com>.
> As far as I understand, this will also restore the _pcVersionInit and
others, isn't?
_pcVersionInit is a flag that gets set to true when the version field is
set. It isn't of any use if you have a DetachedStateManager. HTH.

Thanks,
Rick

On Fri, Jan 14, 2011 at 11:14 AM, Mark Struberg <st...@yahoo.de> wrote:

> btw, I found the default difference between a 'normally' detached entity
> and a serialisation/deserialisation detached entity pretty heavy.
>
> This introduced several errors which were pretty hard to explain to an
> average-joe developer. An application by default might behave completely
> different if you run it on a local tomcat or in a cluster environment (with
> sessions getting replicated via memcached all the times). Another example
> would be using the JSF @ViewScoped (JSF2 ViewMap must get serialized with
> every request) vs e.g. @RequestScoped.
>
> Because of that I'm currently in the process of switching to
> openjpa.DetachState=loaded(DetachedStateField=true)
> which hopefully will remove this obscurities.
> As far as I understand, this will also restore the _pcVersionInit and
> others, isn't?
>
> LieGrue,
> strub
>
>
> --- On Fri, 1/14/11, Rick Curtis <cu...@gmail.com> wrote:
>
> > From: Rick Curtis <cu...@gmail.com>
> > Subject: Re: How to persist duplicate of an entity?
> > To: users@openjpa.apache.org
> > Date: Friday, January 14, 2011, 5:03 PM
> > > OpenJPA considers the entity a
> > detached object even if you reset the Id
> > and version field.
> > In the absence of some sort of StateManager (Impl or
> > Detached) we use the
> > version field and another enhancer added field to detect
> > whether an Entity
> > is new or detached.
> >
> > > Might be a bit too stringent and the error message is
> > misleading (read
> > wrong).
> > Misleading, probably.
> >
> > The problem is that we key off the fact that the version
> > field was set. @See
> > _pcVersionInit in an enhanced Entity. This field is used to
> > differentiate
> > between a uninitialized primitive and a primitive that was
> > actually set to
> > the default value (ie: setting an int version to zero). One
> > way to get
> > around this would be to serialize/deserialize an Entity
> > after setting the
> > version field. Or you could use reflection to set
> > _pcVersionInit to
> > false.... but neither options are good ones.
> >
> > This is a use case is one that wasn't considered when this
> > code was written.
> >
> >
> > Thanks,
> > Rick..
> >
> > On Fri, Jan 14, 2011 at 10:47 AM, Mark Struberg <st...@yahoo.de>
> > wrote:
> >
> > > I think this is perfectly ok with managed entities. It
> > might of course work
> > > with detached ones. Just remember that JPA is a ORM
> > mapper. Thus a primary
> > > key usually must not get changed.
> > >
> > > LieGrue,
> > > strub
> > >
> > > --- On Fri, 1/14/11, Kevin Sutter <kw...@gmail.com>
> > wrote:
> > >
> > > > From: Kevin Sutter <kw...@gmail.com>
> > > > Subject: Re: How to persist duplicate of an
> > entity?
> > > > To: users@openjpa.apache.org
> > > > Date: Friday, January 14, 2011, 4:20 PM
> > > > You are right, Jerry...  It
> > > > looks like some recent changes to detect version
> > > > field updates are preventing this type of
> > processing.
> > > > Even though the
> > > > exception text makes it sound like we should be
> > able to get
> > > > around it...
> > > >
> > > > <openjpa-2.2.0-SNAPSHOT-r422266:1057901M
> > nonfatal store
> > > > error>
> > > >
> > org.apache.openjpa.persistence.EntityExistsException:
> > > > Attempt to persist
> > > > detached object
> > "simple.model.Person@105a105a".  If
> > > > this is a new instance,
> > > > make sure any version and/or auto-generated
> > primary key
> > > > fields are
> > > > null/default when persisting.
> > > > FailedObject: simple.model.Person@105a105a
> > > >
> > > > I tried with and without a version field.
> > And, I
> > > > tried with both primitive
> > > > int and Integer version fields.  No matter
> > what I
> > > > tried, I hit this
> > > > exception either on the first or second commit of
> > the
> > > > Person instance.  Even
> > > > though I am not using auto-generated keys and my
> > version
> > > > field is either
> > > > nulled out or zeroed out.
> > > >
> > > > Maybe this check is too stringent?
> > > >
> > > > Anyway, it sounds like this short cut won't work
> > for
> > > > Ognjen.  Thanks for
> > > > keeping me honest.
> > > >
> > > > Kevin
> > > >
> > > >
> > > > On Fri, Jan 14, 2011 at 9:25 AM, No1UNo <je...@jerrycarter.org>
> > > > wrote:
> > > >
> > > > >
> > > > > In a recent application, I was forced to
> > resort to a
> > > > copy constructor to
> > > > > explicitly make a copy of the object.
> > I tried to
> > > > reuse and re-persist an
> > > > > entity after making a few changes, but any
> > attempt to
> > > > change the value of
> > > > > the @Id was blocked by OpenJPA.
> > > > >
> > > > > On Jan 14, 2011, at 10:19 AM, Kevin Sutter
> > [via
> > > > OpenJPA] wrote:
> > > > >
> > > > > > There are so many variables that can
> > come into
> > > > play with a scenario like
> > > > > > this...
> > > > > >
> > > > > > If you are only looking to populate the
> > database
> > > > and you don't care about
> > > > > > the actual Entity objects, then you
> > could just
> > > > populate a single Entity
> > > > > > instance.  In a loop, persist and
> > commit
> > > > this entity, update the key and
> > > > > > repeat.  If you are using a
> > Version field,
> > > > then you'll also have to reset
> > > > > > that each time.
> > > > > >
> > > > > > It all depends on the end game...
> > :-)
> > > > > >
> > > > > > Kevin
> > > > > >
> > > > > > On Fri, Jan 14, 2011 at 8:52 AM, Henno
> > Vermeulen
> > > > <[hidden email]>wrote:
> > > > > >
> > > > > > > The easiest way that I found to
> > create a
> > > > deep clone of an object graph
> > > > > is
> > > > > > > to use apache commons
> > collections.
> > > > > > >
> > > >
> > SerializationUtils.deserialize(SerializationUtils.serialize(object)).
> > > > > > >
> > > > > > > Can of course also be used for a
> > simple
> > > > entity without relations. In
> > > > > anyway
> > > > > > > you have to watch out that you
> > still set all
> > > > primary keys to null. (May
> > > > > also
> > > > > > > not be most performant way.)
> > > > > > >
> > > > > > > Regards,
> > > > > > > Henno Vermeulen
> > > > > > > Huize Molenaar
> > > > > > >
> > > > > > > -----Oorspronkelijk bericht-----
> > > > > > > Van: Ognjen Blagojevic
> > [mailto:[hidden
> > > > email]]
> > > > > > > Verzonden: vrijdag 14 januari 2011
> > 15:12
> > > > > > > Aan: [hidden email]
> > > > > > > Onderwerp: How to persist
> > duplicate of an
> > > > entity?
> > > > > > >
> > > > > > > Hi,
> > > > > > >
> > > > > > > Is there an easy way to create
> > duplicate of
> > > > an entity?
> > > > > > >
> > > > > > > I want to read entity from the DB,
> > and then
> > > > persist (almost) identical
> > > > > > > copy. Every field should be the
> > same, except
> > > > primary key.
> > > > > > >
> > > > > > > I know I could probably use
> > > > BeanUtils.copyProperties, but is there
> > > > > > > OpenJPA preffered way to do the
> > > > duplication?
> > > > > > >
> > > > > > > Regards,
> > > > > > > Ognjen
> > >
> > >
> > >
> > >
> >
>
>
>
>

Re: How to persist duplicate of an entity?

Posted by Mark Struberg <st...@yahoo.de>.
btw, I found the default difference between a 'normally' detached entity and a serialisation/deserialisation detached entity pretty heavy.

This introduced several errors which were pretty hard to explain to an average-joe developer. An application by default might behave completely different if you run it on a local tomcat or in a cluster environment (with sessions getting replicated via memcached all the times). Another example would be using the JSF @ViewScoped (JSF2 ViewMap must get serialized with every request) vs e.g. @RequestScoped.

Because of that I'm currently in the process of switching to 
openjpa.DetachState=loaded(DetachedStateField=true)
which hopefully will remove this obscurities.
As far as I understand, this will also restore the _pcVersionInit and others, isn't?

LieGrue,
strub


--- On Fri, 1/14/11, Rick Curtis <cu...@gmail.com> wrote:

> From: Rick Curtis <cu...@gmail.com>
> Subject: Re: How to persist duplicate of an entity?
> To: users@openjpa.apache.org
> Date: Friday, January 14, 2011, 5:03 PM
> > OpenJPA considers the entity a
> detached object even if you reset the Id
> and version field.
> In the absence of some sort of StateManager (Impl or
> Detached) we use the
> version field and another enhancer added field to detect
> whether an Entity
> is new or detached.
> 
> > Might be a bit too stringent and the error message is
> misleading (read
> wrong).
> Misleading, probably.
> 
> The problem is that we key off the fact that the version
> field was set. @See
> _pcVersionInit in an enhanced Entity. This field is used to
> differentiate
> between a uninitialized primitive and a primitive that was
> actually set to
> the default value (ie: setting an int version to zero). One
> way to get
> around this would be to serialize/deserialize an Entity
> after setting the
> version field. Or you could use reflection to set
> _pcVersionInit to
> false.... but neither options are good ones.
> 
> This is a use case is one that wasn't considered when this
> code was written.
> 
> 
> Thanks,
> Rick..
> 
> On Fri, Jan 14, 2011 at 10:47 AM, Mark Struberg <st...@yahoo.de>
> wrote:
> 
> > I think this is perfectly ok with managed entities. It
> might of course work
> > with detached ones. Just remember that JPA is a ORM
> mapper. Thus a primary
> > key usually must not get changed.
> >
> > LieGrue,
> > strub
> >
> > --- On Fri, 1/14/11, Kevin Sutter <kw...@gmail.com>
> wrote:
> >
> > > From: Kevin Sutter <kw...@gmail.com>
> > > Subject: Re: How to persist duplicate of an
> entity?
> > > To: users@openjpa.apache.org
> > > Date: Friday, January 14, 2011, 4:20 PM
> > > You are right, Jerry...  It
> > > looks like some recent changes to detect version
> > > field updates are preventing this type of
> processing.
> > > Even though the
> > > exception text makes it sound like we should be
> able to get
> > > around it...
> > >
> > > <openjpa-2.2.0-SNAPSHOT-r422266:1057901M
> nonfatal store
> > > error>
> > >
> org.apache.openjpa.persistence.EntityExistsException:
> > > Attempt to persist
> > > detached object
> "simple.model.Person@105a105a".  If
> > > this is a new instance,
> > > make sure any version and/or auto-generated
> primary key
> > > fields are
> > > null/default when persisting.
> > > FailedObject: simple.model.Person@105a105a
> > >
> > > I tried with and without a version field. 
> And, I
> > > tried with both primitive
> > > int and Integer version fields.  No matter
> what I
> > > tried, I hit this
> > > exception either on the first or second commit of
> the
> > > Person instance.  Even
> > > though I am not using auto-generated keys and my
> version
> > > field is either
> > > nulled out or zeroed out.
> > >
> > > Maybe this check is too stringent?
> > >
> > > Anyway, it sounds like this short cut won't work
> for
> > > Ognjen.  Thanks for
> > > keeping me honest.
> > >
> > > Kevin
> > >
> > >
> > > On Fri, Jan 14, 2011 at 9:25 AM, No1UNo <je...@jerrycarter.org>
> > > wrote:
> > >
> > > >
> > > > In a recent application, I was forced to
> resort to a
> > > copy constructor to
> > > > explicitly make a copy of the object. 
> I tried to
> > > reuse and re-persist an
> > > > entity after making a few changes, but any
> attempt to
> > > change the value of
> > > > the @Id was blocked by OpenJPA.
> > > >
> > > > On Jan 14, 2011, at 10:19 AM, Kevin Sutter
> [via
> > > OpenJPA] wrote:
> > > >
> > > > > There are so many variables that can
> come into
> > > play with a scenario like
> > > > > this...
> > > > >
> > > > > If you are only looking to populate the
> database
> > > and you don't care about
> > > > > the actual Entity objects, then you
> could just
> > > populate a single Entity
> > > > > instance.  In a loop, persist and
> commit
> > > this entity, update the key and
> > > > > repeat.  If you are using a
> Version field,
> > > then you'll also have to reset
> > > > > that each time.
> > > > >
> > > > > It all depends on the end game... 
> :-)
> > > > >
> > > > > Kevin
> > > > >
> > > > > On Fri, Jan 14, 2011 at 8:52 AM, Henno
> Vermeulen
> > > <[hidden email]>wrote:
> > > > >
> > > > > > The easiest way that I found to
> create a
> > > deep clone of an object graph
> > > > is
> > > > > > to use apache commons
> collections.
> > > > > >
> > >
> SerializationUtils.deserialize(SerializationUtils.serialize(object)).
> > > > > >
> > > > > > Can of course also be used for a
> simple
> > > entity without relations. In
> > > > anyway
> > > > > > you have to watch out that you
> still set all
> > > primary keys to null. (May
> > > > also
> > > > > > not be most performant way.)
> > > > > >
> > > > > > Regards,
> > > > > > Henno Vermeulen
> > > > > > Huize Molenaar
> > > > > >
> > > > > > -----Oorspronkelijk bericht-----
> > > > > > Van: Ognjen Blagojevic
> [mailto:[hidden
> > > email]]
> > > > > > Verzonden: vrijdag 14 januari 2011
> 15:12
> > > > > > Aan: [hidden email]
> > > > > > Onderwerp: How to persist
> duplicate of an
> > > entity?
> > > > > >
> > > > > > Hi,
> > > > > >
> > > > > > Is there an easy way to create
> duplicate of
> > > an entity?
> > > > > >
> > > > > > I want to read entity from the DB,
> and then
> > > persist (almost) identical
> > > > > > copy. Every field should be the
> same, except
> > > primary key.
> > > > > >
> > > > > > I know I could probably use
> > > BeanUtils.copyProperties, but is there
> > > > > > OpenJPA preffered way to do the
> > > duplication?
> > > > > >
> > > > > > Regards,
> > > > > > Ognjen
> >
> >
> >
> >
> 


      

Re: How to persist duplicate of an entity?

Posted by Rick Curtis <cu...@gmail.com>.
> OpenJPA considers the entity a detached object even if you reset the Id
and version field.
In the absence of some sort of StateManager (Impl or Detached) we use the
version field and another enhancer added field to detect whether an Entity
is new or detached.

> Might be a bit too stringent and the error message is misleading (read
wrong).
Misleading, probably.

The problem is that we key off the fact that the version field was set. @See
_pcVersionInit in an enhanced Entity. This field is used to differentiate
between a uninitialized primitive and a primitive that was actually set to
the default value (ie: setting an int version to zero). One way to get
around this would be to serialize/deserialize an Entity after setting the
version field. Or you could use reflection to set _pcVersionInit to
false.... but neither options are good ones.

This is a use case is one that wasn't considered when this code was written.


Thanks,
Rick..

On Fri, Jan 14, 2011 at 10:47 AM, Mark Struberg <st...@yahoo.de> wrote:

> I think this is perfectly ok with managed entities. It might of course work
> with detached ones. Just remember that JPA is a ORM mapper. Thus a primary
> key usually must not get changed.
>
> LieGrue,
> strub
>
> --- On Fri, 1/14/11, Kevin Sutter <kw...@gmail.com> wrote:
>
> > From: Kevin Sutter <kw...@gmail.com>
> > Subject: Re: How to persist duplicate of an entity?
> > To: users@openjpa.apache.org
> > Date: Friday, January 14, 2011, 4:20 PM
> > You are right, Jerry...  It
> > looks like some recent changes to detect version
> > field updates are preventing this type of processing.
> > Even though the
> > exception text makes it sound like we should be able to get
> > around it...
> >
> > <openjpa-2.2.0-SNAPSHOT-r422266:1057901M nonfatal store
> > error>
> > org.apache.openjpa.persistence.EntityExistsException:
> > Attempt to persist
> > detached object "simple.model.Person@105a105a".  If
> > this is a new instance,
> > make sure any version and/or auto-generated primary key
> > fields are
> > null/default when persisting.
> > FailedObject: simple.model.Person@105a105a
> >
> > I tried with and without a version field.  And, I
> > tried with both primitive
> > int and Integer version fields.  No matter what I
> > tried, I hit this
> > exception either on the first or second commit of the
> > Person instance.  Even
> > though I am not using auto-generated keys and my version
> > field is either
> > nulled out or zeroed out.
> >
> > Maybe this check is too stringent?
> >
> > Anyway, it sounds like this short cut won't work for
> > Ognjen.  Thanks for
> > keeping me honest.
> >
> > Kevin
> >
> >
> > On Fri, Jan 14, 2011 at 9:25 AM, No1UNo <je...@jerrycarter.org>
> > wrote:
> >
> > >
> > > In a recent application, I was forced to resort to a
> > copy constructor to
> > > explicitly make a copy of the object.  I tried to
> > reuse and re-persist an
> > > entity after making a few changes, but any attempt to
> > change the value of
> > > the @Id was blocked by OpenJPA.
> > >
> > > On Jan 14, 2011, at 10:19 AM, Kevin Sutter [via
> > OpenJPA] wrote:
> > >
> > > > There are so many variables that can come into
> > play with a scenario like
> > > > this...
> > > >
> > > > If you are only looking to populate the database
> > and you don't care about
> > > > the actual Entity objects, then you could just
> > populate a single Entity
> > > > instance.  In a loop, persist and commit
> > this entity, update the key and
> > > > repeat.  If you are using a Version field,
> > then you'll also have to reset
> > > > that each time.
> > > >
> > > > It all depends on the end game...  :-)
> > > >
> > > > Kevin
> > > >
> > > > On Fri, Jan 14, 2011 at 8:52 AM, Henno Vermeulen
> > <[hidden email]>wrote:
> > > >
> > > > > The easiest way that I found to create a
> > deep clone of an object graph
> > > is
> > > > > to use apache commons collections.
> > > > >
> > SerializationUtils.deserialize(SerializationUtils.serialize(object)).
> > > > >
> > > > > Can of course also be used for a simple
> > entity without relations. In
> > > anyway
> > > > > you have to watch out that you still set all
> > primary keys to null. (May
> > > also
> > > > > not be most performant way.)
> > > > >
> > > > > Regards,
> > > > > Henno Vermeulen
> > > > > Huize Molenaar
> > > > >
> > > > > -----Oorspronkelijk bericht-----
> > > > > Van: Ognjen Blagojevic [mailto:[hidden
> > email]]
> > > > > Verzonden: vrijdag 14 januari 2011 15:12
> > > > > Aan: [hidden email]
> > > > > Onderwerp: How to persist duplicate of an
> > entity?
> > > > >
> > > > > Hi,
> > > > >
> > > > > Is there an easy way to create duplicate of
> > an entity?
> > > > >
> > > > > I want to read entity from the DB, and then
> > persist (almost) identical
> > > > > copy. Every field should be the same, except
> > primary key.
> > > > >
> > > > > I know I could probably use
> > BeanUtils.copyProperties, but is there
> > > > > OpenJPA preffered way to do the
> > duplication?
> > > > >
> > > > > Regards,
> > > > > Ognjen
>
>
>
>

Re: How to persist duplicate of an entity?

Posted by Michael Dick <mi...@gmail.com>.
On Fri, Jan 14, 2011 at 10:20 AM, Kevin Sutter <kw...@gmail.com> wrote:

> You are right, Jerry...  It looks like some recent changes to detect
> version
> field updates are preventing this type of processing.  Even though the
> exception text makes it sound like we should be able to get around it...
>
> <openjpa-2.2.0-SNAPSHOT-r422266:1057901M nonfatal store error>
> org.apache.openjpa.persistence.EntityExistsException: Attempt to persist
> detached object "simple.model.Person@105a105a".  If this is a new
> instance,
> make sure any version and/or auto-generated primary key fields are
> null/default when persisting.
> FailedObject: simple.model.Person@105a105a
>
> I tried with and without a version field.  And, I tried with both primitive
> int and Integer version fields.  No matter what I tried, I hit this
> exception either on the first or second commit of the Person instance.
>  Even
> though I am not using auto-generated keys and my version field is either
> nulled out or zeroed out.
>
> Maybe this check is too stringent?


Found this in the spec in the section about persist:

• If X is a detached object, the EntityExistsException may be thrown when
the persist
operation is invoked, or the EntityExistsException or another
PersistenceException
may be thrown at flush or commit time.

OpenJPA considers the entity a detached object even if you reset the Id and
version field. Might be a bit too stringent and the error message is
misleading (read wrong).

-mike


>
> Anyway, it sounds like this short cut won't work for Ognjen.  Thanks for
> keeping me honest.
>
> Kevin
>
>
> On Fri, Jan 14, 2011 at 9:25 AM, No1UNo <je...@jerrycarter.org> wrote:
>
> >
> > In a recent application, I was forced to resort to a copy constructor to
> > explicitly make a copy of the object.  I tried to reuse and re-persist an
> > entity after making a few changes, but any attempt to change the value of
> > the @Id was blocked by OpenJPA.
> >
> > On Jan 14, 2011, at 10:19 AM, Kevin Sutter [via OpenJPA] wrote:
> >
> > > There are so many variables that can come into play with a scenario
> like
> > > this...
> > >
> > > If you are only looking to populate the database and you don't care
> about
> > > the actual Entity objects, then you could just populate a single Entity
> > > instance.  In a loop, persist and commit this entity, update the key
> and
> > > repeat.  If you are using a Version field, then you'll also have to
> reset
> > > that each time.
> > >
> > > It all depends on the end game...  :-)
> > >
> > > Kevin
> > >
> > > On Fri, Jan 14, 2011 at 8:52 AM, Henno Vermeulen <[hidden email]>wrote:
> > >
> > > > The easiest way that I found to create a deep clone of an object
> graph
> > is
> > > > to use apache commons collections.
> > > > SerializationUtils.deserialize(SerializationUtils.serialize(object)).
> > > >
> > > > Can of course also be used for a simple entity without relations. In
> > anyway
> > > > you have to watch out that you still set all primary keys to null.
> (May
> > also
> > > > not be most performant way.)
> > > >
> > > > Regards,
> > > > Henno Vermeulen
> > > > Huize Molenaar
> > > >
> > > > -----Oorspronkelijk bericht-----
> > > > Van: Ognjen Blagojevic [mailto:[hidden email]]
> > > > Verzonden: vrijdag 14 januari 2011 15:12
> > > > Aan: [hidden email]
> > > > Onderwerp: How to persist duplicate of an entity?
> > > >
> > > > Hi,
> > > >
> > > > Is there an easy way to create duplicate of an entity?
> > > >
> > > > I want to read entity from the DB, and then persist (almost)
> identical
> > > > copy. Every field should be the same, except primary key.
> > > >
> > > > I know I could probably use BeanUtils.copyProperties, but is there
> > > > OpenJPA preffered way to do the duplication?
> > > >
> > > > Regards,
> > > > Ognjen
> > > >
> > >
> > >
> > > View message @
> >
> http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922091.html
> > > To start a new topic under OpenJPA Users, email
> > ml-node+208411-1703014788-244680@n2.nabble.com<ml...@n2.nabble.com>
> <ml...@n2.nabble.com>
> >
> > > To unsubscribe from OpenJPA Users, click here.
> >
> >
> > --
> > View this message in context:
> >
> http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922119.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
>

Re: How to persist duplicate of an entity?

Posted by No1UNo <je...@jerrycarter.org>.
For the most part, I approve of steps to detect common user errors.  Both @Id and @Version are special fields that a developer should not touch in normal operation.  The JPA specification does detail how implementations should detect entities that have been persisted, so the current behavior is fine.

That stated, I would _love_ to have function that would 'reset' a detached entity.  Perhaps something like

	em.detach(myObj);  // ensure that the entity has been detached.
	OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
	kem.reset(myObj);

with the result of the 'reset' operation being a class which is again virgin, i.e.

	myObj.id == 0
	myObj.version = 0
	myObj.pcDetachedState == null
	myObj.pcStateManager == null

and so forth for any children.

On Jan 14, 2011, at 11:48 AM, Mark Struberg [via OpenJPA] wrote:

> I think this is perfectly ok with managed entities. It might of course work with detached ones. Just remember that JPA is a ORM mapper. Thus a primary key usually must not get changed. 
> 
> LieGrue, 
> strub 
> 
> --- On Fri, 1/14/11, Kevin Sutter <[hidden email]> wrote: 
> 
> > From: Kevin Sutter <[hidden email]> 
> > Subject: Re: How to persist duplicate of an entity? 
> > To: [hidden email] 
> > Date: Friday, January 14, 2011, 4:20 PM 
> > You are right, Jerry...  It 
> > looks like some recent changes to detect version 
> > field updates are preventing this type of processing.  
> > Even though the 
> > exception text makes it sound like we should be able to get 
> > around it... 
> > 
> > <openjpa-2.2.0-SNAPSHOT-r422266:1057901M nonfatal store 
> > error> 
> > org.apache.openjpa.persistence.EntityExistsException: 
> > Attempt to persist 
> > detached object "simple.model.Person@105a105a".  If 
> > this is a new instance, 
> > make sure any version and/or auto-generated primary key 
> > fields are 
> > null/default when persisting. 
> > FailedObject: simple.model.Person@105a105a 
> > 
> > I tried with and without a version field.  And, I 
> > tried with both primitive 
> > int and Integer version fields.  No matter what I 
> > tried, I hit this 
> > exception either on the first or second commit of the 
> > Person instance.  Even 
> > though I am not using auto-generated keys and my version 
> > field is either 
> > nulled out or zeroed out. 
> > 
> > Maybe this check is too stringent? 
> > 
> > Anyway, it sounds like this short cut won't work for 
> > Ognjen.  Thanks for 
> > keeping me honest. 
> > 
> > Kevin 
> > 
> > 
> > On Fri, Jan 14, 2011 at 9:25 AM, No1UNo <[hidden email]> 
> > wrote: 
> > 
> > > 
> > > In a recent application, I was forced to resort to a 
> > copy constructor to 
> > > explicitly make a copy of the object.  I tried to 
> > reuse and re-persist an 
> > > entity after making a few changes, but any attempt to 
> > change the value of 
> > > the @Id was blocked by OpenJPA. 
> > > 
> > > On Jan 14, 2011, at 10:19 AM, Kevin Sutter [via 
> > OpenJPA] wrote: 
> > > 
> > > > There are so many variables that can come into 
> > play with a scenario like 
> > > > this... 
> > > > 
> > > > If you are only looking to populate the database 
> > and you don't care about 
> > > > the actual Entity objects, then you could just 
> > populate a single Entity 
> > > > instance.  In a loop, persist and commit 
> > this entity, update the key and 
> > > > repeat.  If you are using a Version field, 
> > then you'll also have to reset 
> > > > that each time. 
> > > > 
> > > > It all depends on the end game...  :-) 
> > > > 
> > > > Kevin 
> > > > 
> > > > On Fri, Jan 14, 2011 at 8:52 AM, Henno Vermeulen 
> > <[hidden email]>wrote: 
> > > > 
> > > > > The easiest way that I found to create a 
> > deep clone of an object graph 
> > > is 
> > > > > to use apache commons collections. 
> > > > > 
> > SerializationUtils.deserialize(SerializationUtils.serialize(object)). 
> > > > > 
> > > > > Can of course also be used for a simple 
> > entity without relations. In 
> > > anyway 
> > > > > you have to watch out that you still set all 
> > primary keys to null. (May 
> > > also 
> > > > > not be most performant way.) 
> > > > > 
> > > > > Regards, 
> > > > > Henno Vermeulen 
> > > > > Huize Molenaar 
> > > > > 
> > > > > -----Oorspronkelijk bericht----- 
> > > > > Van: Ognjen Blagojevic [mailto:[hidden 
> > email]] 
> > > > > Verzonden: vrijdag 14 januari 2011 15:12 
> > > > > Aan: [hidden email] 
> > > > > Onderwerp: How to persist duplicate of an 
> > entity? 
> > > > > 
> > > > > Hi, 
> > > > > 
> > > > > Is there an easy way to create duplicate of 
> > an entity? 
> > > > > 
> > > > > I want to read entity from the DB, and then 
> > persist (almost) identical 
> > > > > copy. Every field should be the same, except 
> > primary key. 
> > > > > 
> > > > > I know I could probably use 
> > BeanUtils.copyProperties, but is there 
> > > > > OpenJPA preffered way to do the 
> > duplication? 
> > > > > 
> > > > > Regards, 
> > > > > Ognjen 
> > > > > 
> > > > 
> > > > 
> > > > View message @ 
> > > http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922091.html
> > > > To start a new topic under OpenJPA Users, email 
> > > [hidden email]<[hidden email]> 
> > > > To unsubscribe from OpenJPA Users, click here. 
> > > 
> > > 
> > > -- 
> > > View this message in context: 
> > > http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922119.html
> > > Sent from the OpenJPA Users mailing list archive at 
> > Nabble.com. 
> > > 
> >
> 
> 
> 
> 
> 
> View message @ http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922509.html
> To start a new topic under OpenJPA Users, email ml-node+208411-1703014788-244680@n2.nabble.com 
> To unsubscribe from OpenJPA Users, click here.


-- 
View this message in context: http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922558.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: How to persist duplicate of an entity?

Posted by Mark Struberg <st...@yahoo.de>.
I think this is perfectly ok with managed entities. It might of course work with detached ones. Just remember that JPA is a ORM mapper. Thus a primary key usually must not get changed.

LieGrue,
strub

--- On Fri, 1/14/11, Kevin Sutter <kw...@gmail.com> wrote:

> From: Kevin Sutter <kw...@gmail.com>
> Subject: Re: How to persist duplicate of an entity?
> To: users@openjpa.apache.org
> Date: Friday, January 14, 2011, 4:20 PM
> You are right, Jerry...  It
> looks like some recent changes to detect version
> field updates are preventing this type of processing. 
> Even though the
> exception text makes it sound like we should be able to get
> around it...
> 
> <openjpa-2.2.0-SNAPSHOT-r422266:1057901M nonfatal store
> error>
> org.apache.openjpa.persistence.EntityExistsException:
> Attempt to persist
> detached object "simple.model.Person@105a105a".  If
> this is a new instance,
> make sure any version and/or auto-generated primary key
> fields are
> null/default when persisting.
> FailedObject: simple.model.Person@105a105a
> 
> I tried with and without a version field.  And, I
> tried with both primitive
> int and Integer version fields.  No matter what I
> tried, I hit this
> exception either on the first or second commit of the
> Person instance.  Even
> though I am not using auto-generated keys and my version
> field is either
> nulled out or zeroed out.
> 
> Maybe this check is too stringent?
> 
> Anyway, it sounds like this short cut won't work for
> Ognjen.  Thanks for
> keeping me honest.
> 
> Kevin
> 
> 
> On Fri, Jan 14, 2011 at 9:25 AM, No1UNo <je...@jerrycarter.org>
> wrote:
> 
> >
> > In a recent application, I was forced to resort to a
> copy constructor to
> > explicitly make a copy of the object.  I tried to
> reuse and re-persist an
> > entity after making a few changes, but any attempt to
> change the value of
> > the @Id was blocked by OpenJPA.
> >
> > On Jan 14, 2011, at 10:19 AM, Kevin Sutter [via
> OpenJPA] wrote:
> >
> > > There are so many variables that can come into
> play with a scenario like
> > > this...
> > >
> > > If you are only looking to populate the database
> and you don't care about
> > > the actual Entity objects, then you could just
> populate a single Entity
> > > instance.  In a loop, persist and commit
> this entity, update the key and
> > > repeat.  If you are using a Version field,
> then you'll also have to reset
> > > that each time.
> > >
> > > It all depends on the end game...  :-)
> > >
> > > Kevin
> > >
> > > On Fri, Jan 14, 2011 at 8:52 AM, Henno Vermeulen
> <[hidden email]>wrote:
> > >
> > > > The easiest way that I found to create a
> deep clone of an object graph
> > is
> > > > to use apache commons collections.
> > > >
> SerializationUtils.deserialize(SerializationUtils.serialize(object)).
> > > >
> > > > Can of course also be used for a simple
> entity without relations. In
> > anyway
> > > > you have to watch out that you still set all
> primary keys to null. (May
> > also
> > > > not be most performant way.)
> > > >
> > > > Regards,
> > > > Henno Vermeulen
> > > > Huize Molenaar
> > > >
> > > > -----Oorspronkelijk bericht-----
> > > > Van: Ognjen Blagojevic [mailto:[hidden
> email]]
> > > > Verzonden: vrijdag 14 januari 2011 15:12
> > > > Aan: [hidden email]
> > > > Onderwerp: How to persist duplicate of an
> entity?
> > > >
> > > > Hi,
> > > >
> > > > Is there an easy way to create duplicate of
> an entity?
> > > >
> > > > I want to read entity from the DB, and then
> persist (almost) identical
> > > > copy. Every field should be the same, except
> primary key.
> > > >
> > > > I know I could probably use
> BeanUtils.copyProperties, but is there
> > > > OpenJPA preffered way to do the
> duplication?
> > > >
> > > > Regards,
> > > > Ognjen
> > > >
> > >
> > >
> > > View message @
> > http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922091.html
> > > To start a new topic under OpenJPA Users, email
> > ml-node+208411-1703014788-244680@n2.nabble.com<ml...@n2.nabble.com>
> > > To unsubscribe from OpenJPA Users, click here.
> >
> >
> > --
> > View this message in context:
> > http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922119.html
> > Sent from the OpenJPA Users mailing list archive at
> Nabble.com.
> >
> 


      

Re: How to persist duplicate of an entity?

Posted by Kevin Sutter <kw...@gmail.com>.
You are right, Jerry...  It looks like some recent changes to detect version
field updates are preventing this type of processing.  Even though the
exception text makes it sound like we should be able to get around it...

<openjpa-2.2.0-SNAPSHOT-r422266:1057901M nonfatal store error>
org.apache.openjpa.persistence.EntityExistsException: Attempt to persist
detached object "simple.model.Person@105a105a".  If this is a new instance,
make sure any version and/or auto-generated primary key fields are
null/default when persisting.
FailedObject: simple.model.Person@105a105a

I tried with and without a version field.  And, I tried with both primitive
int and Integer version fields.  No matter what I tried, I hit this
exception either on the first or second commit of the Person instance.  Even
though I am not using auto-generated keys and my version field is either
nulled out or zeroed out.

Maybe this check is too stringent?

Anyway, it sounds like this short cut won't work for Ognjen.  Thanks for
keeping me honest.

Kevin


On Fri, Jan 14, 2011 at 9:25 AM, No1UNo <je...@jerrycarter.org> wrote:

>
> In a recent application, I was forced to resort to a copy constructor to
> explicitly make a copy of the object.  I tried to reuse and re-persist an
> entity after making a few changes, but any attempt to change the value of
> the @Id was blocked by OpenJPA.
>
> On Jan 14, 2011, at 10:19 AM, Kevin Sutter [via OpenJPA] wrote:
>
> > There are so many variables that can come into play with a scenario like
> > this...
> >
> > If you are only looking to populate the database and you don't care about
> > the actual Entity objects, then you could just populate a single Entity
> > instance.  In a loop, persist and commit this entity, update the key and
> > repeat.  If you are using a Version field, then you'll also have to reset
> > that each time.
> >
> > It all depends on the end game...  :-)
> >
> > Kevin
> >
> > On Fri, Jan 14, 2011 at 8:52 AM, Henno Vermeulen <[hidden email]>wrote:
> >
> > > The easiest way that I found to create a deep clone of an object graph
> is
> > > to use apache commons collections.
> > > SerializationUtils.deserialize(SerializationUtils.serialize(object)).
> > >
> > > Can of course also be used for a simple entity without relations. In
> anyway
> > > you have to watch out that you still set all primary keys to null. (May
> also
> > > not be most performant way.)
> > >
> > > Regards,
> > > Henno Vermeulen
> > > Huize Molenaar
> > >
> > > -----Oorspronkelijk bericht-----
> > > Van: Ognjen Blagojevic [mailto:[hidden email]]
> > > Verzonden: vrijdag 14 januari 2011 15:12
> > > Aan: [hidden email]
> > > Onderwerp: How to persist duplicate of an entity?
> > >
> > > Hi,
> > >
> > > Is there an easy way to create duplicate of an entity?
> > >
> > > I want to read entity from the DB, and then persist (almost) identical
> > > copy. Every field should be the same, except primary key.
> > >
> > > I know I could probably use BeanUtils.copyProperties, but is there
> > > OpenJPA preffered way to do the duplication?
> > >
> > > Regards,
> > > Ognjen
> > >
> >
> >
> > View message @
> http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922091.html
> > To start a new topic under OpenJPA Users, email
> ml-node+208411-1703014788-244680@n2.nabble.com<ml...@n2.nabble.com>
> > To unsubscribe from OpenJPA Users, click here.
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922119.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: How to persist duplicate of an entity?

Posted by No1UNo <je...@jerrycarter.org>.
In a recent application, I was forced to resort to a copy constructor to explicitly make a copy of the object.  I tried to reuse and re-persist an entity after making a few changes, but any attempt to change the value of the @Id was blocked by OpenJPA.

On Jan 14, 2011, at 10:19 AM, Kevin Sutter [via OpenJPA] wrote:

> There are so many variables that can come into play with a scenario like 
> this... 
> 
> If you are only looking to populate the database and you don't care about 
> the actual Entity objects, then you could just populate a single Entity 
> instance.  In a loop, persist and commit this entity, update the key and 
> repeat.  If you are using a Version field, then you'll also have to reset 
> that each time. 
> 
> It all depends on the end game...  :-) 
> 
> Kevin 
> 
> On Fri, Jan 14, 2011 at 8:52 AM, Henno Vermeulen <[hidden email]>wrote: 
> 
> > The easiest way that I found to create a deep clone of an object graph is 
> > to use apache commons collections. 
> > SerializationUtils.deserialize(SerializationUtils.serialize(object)). 
> > 
> > Can of course also be used for a simple entity without relations. In anyway 
> > you have to watch out that you still set all primary keys to null. (May also 
> > not be most performant way.) 
> > 
> > Regards, 
> > Henno Vermeulen 
> > Huize Molenaar 
> > 
> > -----Oorspronkelijk bericht----- 
> > Van: Ognjen Blagojevic [mailto:[hidden email]] 
> > Verzonden: vrijdag 14 januari 2011 15:12 
> > Aan: [hidden email] 
> > Onderwerp: How to persist duplicate of an entity? 
> > 
> > Hi, 
> > 
> > Is there an easy way to create duplicate of an entity? 
> > 
> > I want to read entity from the DB, and then persist (almost) identical 
> > copy. Every field should be the same, except primary key. 
> > 
> > I know I could probably use BeanUtils.copyProperties, but is there 
> > OpenJPA preffered way to do the duplication? 
> > 
> > Regards, 
> > Ognjen 
> > 
> 
> 
> View message @ http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922091.html
> To start a new topic under OpenJPA Users, email ml-node+208411-1703014788-244680@n2.nabble.com 
> To unsubscribe from OpenJPA Users, click here.


-- 
View this message in context: http://openjpa.208410.n2.nabble.com/How-to-persist-duplicate-of-an-entity-tp5921829p5922119.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: How to persist duplicate of an entity?

Posted by Kevin Sutter <kw...@gmail.com>.
There are so many variables that can come into play with a scenario like
this...

If you are only looking to populate the database and you don't care about
the actual Entity objects, then you could just populate a single Entity
instance.  In a loop, persist and commit this entity, update the key and
repeat.  If you are using a Version field, then you'll also have to reset
that each time.

It all depends on the end game...  :-)

Kevin

On Fri, Jan 14, 2011 at 8:52 AM, Henno Vermeulen <he...@huizemolenaar.nl>wrote:

> The easiest way that I found to create a deep clone of an object graph is
> to use apache commons collections.
> SerializationUtils.deserialize(SerializationUtils.serialize(object)).
>
> Can of course also be used for a simple entity without relations. In anyway
> you have to watch out that you still set all primary keys to null. (May also
> not be most performant way.)
>
> Regards,
> Henno Vermeulen
> Huize Molenaar
>
> -----Oorspronkelijk bericht-----
> Van: Ognjen Blagojevic [mailto:ognjen.d.blagojevic@gmail.com]
> Verzonden: vrijdag 14 januari 2011 15:12
> Aan: users@openjpa.apache.org
> Onderwerp: How to persist duplicate of an entity?
>
> Hi,
>
> Is there an easy way to create duplicate of an entity?
>
> I want to read entity from the DB, and then persist (almost) identical
> copy. Every field should be the same, except primary key.
>
> I know I could probably use BeanUtils.copyProperties, but is there
> OpenJPA preffered way to do the duplication?
>
> Regards,
> Ognjen
>

Re: How to persist duplicate of an entity?

Posted by Ognjen Blagojevic <og...@gmail.com>.
In this particular case I need to create a shallow copy. I am developing 
web application with standard CRUD operations for every table / entity 
class. I was looking for a way to add "duplicate" action.

To sum things up, I tried to:

1. Find entity, change primary key field (int) to 0, and persist. 
Result: "Attempt to change a primary key field of an instance that 
already has a final object id.  Only new, unflushed instances whose id 
you have not retrieved can have their primary keys changed."

2. Find entity, clone(), change primary key field to 0, and persist. 
Result: "ERROR: null value in column "id_some_foreign_key" violates 
not-null constraint (...)"

3. Find entity, BeanUtils.copyProperties, change primary key field to 0, 
persist. This seems to work.

4. Find entity, serialize, deserialize, change primary key field to 0, 
persist, as Henno suggested. This also seems to work, but I have some 
issues with lazy loaded objects.

5. Of course, copy constructor is always an option, but it takes a bit 
more work.

Regards,
Ognjen



On 14.1.2011 15:52, Henno Vermeulen wrote:
> The easiest way that I found to create a deep clone of an object graph is to use apache commons collections.
> SerializationUtils.deserialize(SerializationUtils.serialize(object)).
>
> Can of course also be used for a simple entity without relations. In anyway you have to watch out that you still set all primary keys to null. (May also not be most performant way.)
>
> Regards,
> Henno Vermeulen
> Huize Molenaar
>
> -----Oorspronkelijk bericht-----
> Van: Ognjen Blagojevic [mailto:ognjen.d.blagojevic@gmail.com]
> Verzonden: vrijdag 14 januari 2011 15:12
> Aan: users@openjpa.apache.org
> Onderwerp: How to persist duplicate of an entity?
>
> Hi,
>
> Is there an easy way to create duplicate of an entity?
>
> I want to read entity from the DB, and then persist (almost) identical
> copy. Every field should be the same, except primary key.
>
> I know I could probably use BeanUtils.copyProperties, but is there
> OpenJPA preffered way to do the duplication?
>
> Regards,
> Ognjen
>


RE: How to persist duplicate of an entity?

Posted by Henno Vermeulen <he...@huizemolenaar.nl>.
The easiest way that I found to create a deep clone of an object graph is to use apache commons collections.
SerializationUtils.deserialize(SerializationUtils.serialize(object)).

Can of course also be used for a simple entity without relations. In anyway you have to watch out that you still set all primary keys to null. (May also not be most performant way.)

Regards,
Henno Vermeulen
Huize Molenaar

-----Oorspronkelijk bericht-----
Van: Ognjen Blagojevic [mailto:ognjen.d.blagojevic@gmail.com] 
Verzonden: vrijdag 14 januari 2011 15:12
Aan: users@openjpa.apache.org
Onderwerp: How to persist duplicate of an entity?

Hi,

Is there an easy way to create duplicate of an entity?

I want to read entity from the DB, and then persist (almost) identical 
copy. Every field should be the same, except primary key.

I know I could probably use BeanUtils.copyProperties, but is there 
OpenJPA preffered way to do the duplication?

Regards,
Ognjen

Re: How to persist duplicate of an entity?

Posted by Rick Curtis <cu...@gmail.com>.
Ognjen -

I would think implementing a copy constructor would be your safest best.
Perhaps not the least amount of code, but the safest.

Thanks,
Rick

2011/1/14 Ognjen Blagojevic <og...@gmail.com>

> Hi,
>
> Is there an easy way to create duplicate of an entity?
>
> I want to read entity from the DB, and then persist (almost) identical
> copy. Every field should be the same, except primary key.
>
> I know I could probably use BeanUtils.copyProperties, but is there OpenJPA
> preffered way to do the duplication?
>
> Regards,
> Ognjen
>