Two Laptop Bag*
The moose likes Object Relational Mapping and the fly likes Problem in saveOrUpdate() method in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem in saveOrUpdate() method in hibernate" Watch "Problem in saveOrUpdate() method in hibernate" New topic
Author

Problem in saveOrUpdate() method in hibernate

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I am working on a hibernate project.

Before inserting any record i have to check whether the record is present on DB. If present i have to update it, and if not, save it. On updating it should return 1, an int, and 0 on saving. Based on this return value, it performs some additional functions.


Here is the code


The problem is that even on inserting a new record, it goes to else if block containing the saveOrUpdate().
What is the mistake here?


kunal
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
No reply yet
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
When i am using get() instead of load(), it saves new entity. But when i run the different code for Editing, and it uses the same DAO code as mentioned earlier, it throws following exception

Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Kunal ,

The problem is because of this code


try copying all changes to sp and saveorUpdate it.




Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Hemant Thard, it works. Thanks.

But, i really don't know why the code given by me doesn't works.
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Kunal Lakhani wrote:Hemant Thard, it works. Thanks.

But, i really don't know why the code given by me doesn't works.


The reason is that you cannot have 2 different object having same identifier in hibernate persistence context.

This is what the exception tells you.

NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Hemant Thard
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in saveOrUpdate() method in hibernate
 
Similar Threads
Singleton only creates 1 Instance or more ?
How to display DB table values?
Auxiliary tables saving
Even though id is same records are getting inserted in few table instead of update.
inserting through Hibernate