Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate giving NonUniqueObjectException

 
Saikiran Madhavan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working in a web app with Spring/Hibernate. I am getting the exception NonUniqueObjectException when i try to update my Account Object.

I am using Spring-Hibernate Template and i have written my DAO and tested it.

What i do is - user has to type the email id of an account and if that exists it will show the other details of that account like username, password, email etc.

When user do some changes in the the name,address or password and click the save button the same object has to be saved or updated.

Inorder to Check whether the operation is save or update, i am passing the PK (accountId) while updating the object.

Code is given below (Controller part):-



the high-lighted code is the problem area.

can anyone suggest me where i am doing the mistake.

thanks in advance
saikiran
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK first and foremost, please please please break up that method into more than one method. it is so long that I can't read it. By breaking it up into methods that do one thing and are smaller will make readability and maintainability much easier.

So I am going to help you by just making assumptions on what the code is.

I think you have a detached object, which is sent to the client, it comes back detached and you need to reattach the object. You will need to use a method like merge() because there is another instance of your object within the Session's Persistent state with that object's id.

Here is an example.

Lets say I have a list with an Object in it



Lets just say that a List won't allow you add an object if there is another object in the list with the same id, and we assume that a and b have the same id. The last line of code will fail. This is basically what is happening. Now let's say that a List had a method called merge, that will make is such that you can add an object with the same id, but only though that method

so then myList.merge(b); will not cause an error and would replace object a, which is exactly what you want in your code.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic