Nhibernate loses previous item from AsSet collection when adding the first new one
Description
i have a problem with Nhibernate collection mapped asSet, in few word when i add new item to collection all other item are lost.
but what is more strange is that those appens only once, every following insert give no problem since i restart app
this my object:
`BaseObject` override Equals using ID property
`EquatableObject` override Equals comparing all object property
the maps:
and finally the test:
so when i add the second sal the first is lost but when i add the third the second remain, why??
looking at SQL log i see that when i add the second SAL un update is fired to SAL TABLE settings IdAttivita to NULL, so link to Attivita is lost
i read that removing and inserting all links is behaviour is by design with Bag collection but i have SET with primary key
More strange if i change `AsSet` to `AsBag` (or simply remove `AsAet`) for collection mapping all works fine, but i need Set for other things (like multiple fetch)
Great, i switch SAL to BaseObject to use only Id in Equals and all works fine.
What bring me on wrong way was that only first comparison fail and all others worked
Alex Zaytsev
February 2, 2017 at 8:38 PM
(edited)
In short - you cannot mess with Equals method. Consider chnaging SAL mapping to Component/Element, instead of entity. This is the rule, that entity do equal by ID, components - by values.
i have a problem with Nhibernate collection mapped asSet, in few word when i add new item to collection all other item are lost.
but what is more strange is that those appens only once, every following insert give no problem since i restart app
this my object:
`BaseObject` override Equals using ID property
`EquatableObject` override Equals comparing all object property
the maps:
and finally the test:
so when i add the second sal the first is lost but when i add the third the second remain, why??
looking at SQL log i see that when i add the second SAL un update is fired to SAL TABLE settings IdAttivita to NULL, so link to Attivita is lost
i read that removing and inserting all links is behaviour is by design with Bag collection but i have SET with primary key
More strange if i change `AsSet` to `AsBag` (or simply remove `AsAet`) for collection mapping all works fine, but i need Set for other things (like multiple fetch)
links to my StackOverflow question:
http://stackoverflow.com/questions/42005044/nhibernate-loses-previous-item-from-asset-collection-when-adding-the-first-new-o