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
> [email protected]
> 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.