java - Add new Entity to persisted Collection -
I've got the related table in multiple-to-many associations: Product * 1 - n * Inventory
< Previous> @Entity Public Category Products {// Identifier and Properties ... @OnTomy (fetch = fetchtype.EAGER, cascade = CascadeType.ALL) Public set & lt; Inventory & gt; GetInventories () {Return List; } Public Zero Set Inventory (set & lt; Inventory & gt; inventories) {This list = inventories; } Public Zero addInventory (Inventory Inventory) {this.inventories.add (list); Inventory.setProduct (this); }}
-
@Entity public category list {// identifier and property ... private product product; @ManyToOne (cascade = cascade type.all, alternative = false) public product getProduct () {return product; } Public Zero Set Product (Product Product) {this.product = Product; }}
I have followed the seatement:
- I continue a product with a blank inventory set
- I Loading the product
- I try to update / merge the product
By doing this, I get the exception:
HibernateSystemException: A different object with the same identifier value was already associated with the session
means of exception Land that exists in the session contains an object with the same value of @ id
column, which is not the same thing as the current object.
You have to overwrite hashode ()
and equals ()
on list
(preferably using a business key By which the session will know that the same entity, even if the object example is different.
Comments
Post a Comment