Reputation: 3129
I have a pretty simple tables structure: table Products with product_id and name attributes. table Barcodes with product_id (foreign key to the Products table) and code attributes.
In my code I create new instance of a Product object:
Product product = new Product();
and I save the product:
session.save(product);
Later I create new instance of a Barcode object:
Barcode barcode = new Barcode();
and associate the product with the barcode:
barcode.setProduct(product);
and try to save the barcode:
session.save(barcode);
and I'm getting:
Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
Any ideas?
Upvotes: 0
Views: 859
Reputation: 128779
At least a couple of ideas:
For more ideas, give more details.
Upvotes: 3
Reputation: 15141
Like JB said your mapping files (and the actual code) would really help. For now you can try adding:
session.flush()
After:
session.save(product)
I'm guessing that hibernate is trying to "optimize" this transaction and saves the same product again when you're saving the barcode.
But that's only one possibility. Do you have any loops in your code?
Upvotes: 0