This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan 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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [JPA] merge() is inserting instead of updating
 
Similar Threads
Knowing about the transaction
Stateless session bean with CMT, sending JMS message on commit
Can we write Entity Bean for a table which does not have a Primary Key defined?
How to update an entity when the persistence context is extended?
JPA Delete Entity and Refresh Collections containing Entity