• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate giving NonUniqueObjectException

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic