This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes [JPA] merge() is inserting instead of updating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "[JPA] merge() is inserting instead of updating" Watch "[JPA] merge() is inserting instead of updating" New topic
Author

[JPA] merge() is inserting instead of updating

Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

hi,
can anyone enlighten me regarding the mecanism of persistence in EJB 3.0 ?
I am trying to update an entity in the database from a web client that calls a method in a stateful session bean.
That method looks like this :



It does not update the entity, it inserts a new row in the table.
What do i need to do so that it updates the row ?
Am i missing something regarding the lifecycle of the entity beans ?

Thanks for helping.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
What is the id/primary key of the entity? Is there an existing row in the table with that id? Merge will insert the object if it does not exist, so if you want an update, ensure the object does exist.

You could try first doing a find() on the entity, and ensure it exists before calling the merge.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [JPA] merge() is inserting instead of updating