File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

[JPA] merge() is inserting instead of updating

Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 549

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
I agree. Here's the link:
subject: [JPA] merge() is inserting instead of updating
It's not a secret anymore!