Francois
Francois

Reputation: 10968

nhibernate: remove object from collection without deleting it

Let's say that we have a SPACE_SHIP and WEAPON objects. A SPACE_SHIP has one primary WEAPON and a collection of secondary WEAPON. I'd like to switch primary and one of the secondary WEAPON, how can I do that?

If I do:

  1. mySpaceShip.SecondaryWeapons.Add(mySpaceShip.PrimaryWeapon)
  2. mySpaceShip.PrimaryWeapon = theSecondaryWeaponToSwitch
  3. mySpaceShip.SecondaryWeapons.Remove(theSecondaryWeaponToSwitch)

NHibernate error occurs... What shall I do?

Context: NHibernate 1.2, C# 3.5

Upvotes: 0

Views: 360

Answers (1)

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

You most probably mapped it with cascade="all-delete-orphan". When doing that, NH deletes all instances which had been removed from the collection. If you try to use that instance in another collection, NH complains.

NH doesn't implement "persistent garbage collection" to automatically detect which instances are referenced and which are not. This would be a much too strong performance impact. "delete-orphan" is a simplified version of that, which works in many simple cases, but doesn't work if you move instances around.

Upvotes: 1

Related Questions