Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes Error in Hibernate while merging an object : More than one row with the given identifier was found Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Error in Hibernate while merging an object : More than one row with the given identifier was found " Watch "Error in Hibernate while merging an object : More than one row with the given identifier was found " New topic
Author

Error in Hibernate while merging an object : More than one row with the given identifier was found

Saeed alkhen
Greenhorn

Joined: Feb 11, 2009
Posts: 4
Hi everybody,
I'm facing the following error in Hibernate : More than one row with the given identifier was found while I'm trying to merge the object.

Details :
  • I'm using Spring 2.5, Hibernate 3.0 and my application deployed on Websphere ver 7.x
  • I applied the DB Session per HTTP request methodology (not the spring implementation) but I used the ThreadLocal to save the session to be used again within the current HTTP request, and once the response is ready to be sent to the client I have a HTTPFilter to close any opened session in this Thread.
  • I got the error once I have a bulk operation on the DB within one HTTP request (Deleting, Updating, Merging, Inserting,.....)
  • I used the JNDI datasource managed by Websphere itself
  • I was thinking like following: At the starting point of my request, I asked the session factory to provide me a session instance, and I used this session in my request until I closed it at the end of my request processing. and the hibernate merge method try to load the object before merging it, once the query for loading is executed: the exception is thrown telling me that (More than one row with the given identifier was found), for me this means that the object is loaded to the current session before, so the first stupid idea was to flush and close the current session before merging and start a new one just for this merging, I did that but I faced the same error, that means I was thinking wrong from the beginning.
  • The second approach I'm trying to follow is to separate this bulk operation to multi operations done directly in separate ajax request (Suppose I have a grid with update and delete functionality for each row, I allow the user to update and delete whatever he wants and I cache these changes until the user click "apply" button to apply all thees changes, the new Idea is: once the user tried to delete a row a new HTTP request (with DB session) will be responsible for deleting this row in the database, in this case no more need to the apply button because the modifications a reflected directly to the DB)




  • Please help me.
    Regards

    Vijitha Kumara
    Bartender

    Joined: Mar 24, 2008
    Posts: 3816

    How are the IDs are generated for these inserts? And have you loaded any data outside the application to the database which might have not defined a proper PK for the table?


    SCJP 5 | SCWCD 5
    [How to ask questions] [Twitter]
    Saeed alkhen
    Greenhorn

    Joined: Feb 11, 2009
    Posts: 4
    Dear Vijitha Kumara,
    Thank you very much for your response, For the ID generation: this table has a composite primary key (Department_Id, Correspondence_Site_Id), both of them is foreign key to another table.
    And for your second question: I can't got your idea, could you please repeat it in a clearer way.

    Thanks and best regards,
    Vijitha Kumara
    Bartender

    Joined: Mar 24, 2008
    Posts: 3816

    And for your second question: I can't got your idea, could you please repeat it in a clearer way.

    I asked whether you loaded data to the DB separately which causes this error.

    Can you post the stack trace and perhaps explain a bit about the structure of the offending table(s) and the nature of this "bulk" operation(s)...
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Error in Hibernate while merging an object : More than one row with the given identifier was found
     
    Similar Threads
    [Hibernate] Batch update returned unexpected row count
    id is not set after getSession().save()
    Issue when using 2 entity managers to manage one entity from one database to another
    Deleting a List of Objects with HIbernate
    FATAL pool-4-thread-12 More than one row with the given identifier was found: 3, for class: hierarc