nhibernate:从集合中删除对象而不删除它
假设我们有一个 SPACE_SHIP 和 WEAPON 对象。一艘 SPACE_SHIP 有一个主要武器和一组次要武器。我想切换主 WEAPON 和辅助 WEAPON 之一,我该怎么做?
如果我这样做:
- mySpaceShip.SecondaryWeapons.Add(mySpaceShip.PrimaryWeapon)
- mySpaceShip.PrimaryWeapon = theSecondaryWeaponToSwitch
- mySpaceShip.SecondaryWeapons.Remove(theSecondaryWeaponToSwitch)
发生 NHibernate 错误...我该怎么办?
上下文:NHibernate 1.2、C# 3.5
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:
- mySpaceShip.SecondaryWeapons.Add(mySpaceShip.PrimaryWeapon)
- mySpaceShip.PrimaryWeapon = theSecondaryWeaponToSwitch
- mySpaceShip.SecondaryWeapons.Remove(theSecondaryWeaponToSwitch)
NHibernate error occurs... What shall I do?
Context: NHibernate 1.2, C# 3.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能使用
cascade="all-delete-orphan"
映射它。执行此操作时,NH 会删除已从集合中删除的所有实例。如果您尝试在另一个集合中使用该实例,NH 会抱怨。NH 没有实现“持久垃圾收集”来自动检测哪些实例被引用,哪些实例未被引用。这对性能的影响太大了。 “delete-orphan”是它的简化版本,它适用于许多简单的情况,但如果您移动实例则不起作用。
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.