Different object with the same Identifier

Tag: grails Author: zhangshian Date: 2009-09-05

Any ideas as to why I'd be getting this error:

nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [Product#6]

From this code:

def save = {    
    def productInstance = new Product(params)

    if(!productInstance.hasErrors() && productInstance.save()) {
    	flash.message = "Product ${productInstance.id} created"
    	redirect(action:show,id:productInstance.id)
    }
    else {
        render(view:'create',model:[productInstance:productInstance])
    }
}
need to see the rest of the code on how you rendered the page to accept the initial input
The page is just scaffold at this point.
what does the Product Domain object look like?

Best Answer

It turned out the problem was a bug with the Searchable plugin, which doesn't allow you to index more than one domain. Disabling Searchable in all but one domain class resolved the issue.

This issue is detailed in an issue on Jira here: http://jira.codehaus.org/browse/GRAILSPLUGINS-601

comments:

This issue seems to come up in a lot of different contexts. Check out jira.grails.org/browse/… jira.grails.org/browse/GPSEARCHABLE-35

Other Answer1

You might have been passing id as part of the params, and a product with that id probably already exists in database.

comments:

Nope. The params don't include a product id and the id it gives in the error doesn't exist in the DB until after the form is submitted. It's as though it's trying to insert it twice.
Can you try flush: true, just in case!