This technique is almost correct.

Object o=em.find(Object.class,pk);
then change/ set new values (not the pk)

o=em.merge(o);

You don't want to merge here. The find will put the instance into the PersistenceContext.

Just commit or flush changes to the database.

Craig

On Jul 31, 2007, at 10:29 AM, Mauricio Salatino wrote:

you shouldn't change the pk if you want to do an update..
but if you are with a new object you must attach the object to the session.
so.. the sequence be like this:

if you know the pk of the object

Object o=em.find(Object.class,pk);
then change/ set new values (not the pk)

o=em.merge(o);



On 7/31/07, Tim Hanson <[EMAIL PROTECTED]> wrote:
I somewhat confused by the behaviour of merge(). It seems that if I just new up an entity and set its fields, including the primary key, then I
call em.merge(), INSERT is called instead of UPDATE. Is this true?

I am using JPA with JSF. I have a backing bean with a reference to an
entity. When I try to edit the entity, on save JSF fills in the fields
of the entity from the request and then I call em.merge(), but like I
said, that always calls INSERT. The only way I was able to make this
work was by scoping the backing bean to the session, such that my entity
was alive between calls. That seems like a strange thing to do.

The other possibility would be to find the entity and then copy the
fields. That also seems like a hack.

I guess if merge() is not really UPDATE, is there something else I can
call?

Thanks,
Tim

Notice: This email message, together with any attachments, may contain information of BEA Systems, Inc., its subsidiaries and affiliated entities, that may be confidential, proprietary, copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.



--
 - Salatino Mauricio -

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:[EMAIL PROTECTED]
P.S. A good JDO? O, Gasp!

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to