从父集合中删除子记录
我正在开发一个示例应用程序,以便我可以了解 NHibernate 的细节。 我正在努力解决删除问题。 我希望能够通过将子记录从其父级集合中删除然后保存父级来删除子记录。 我已经设置了双向一对多关系,插入/更新效果很好。
这是我的映射
篮:
BasketItem:
我想打电话basket.RemoveBasketItem(BasketItem item)
然后 Session.SaveUpdate(basket)
以便删除购物篮项目。 这可能吗?
I'm developing a sample application so that I can learn the ins and outs of NHibernate. I am struggling with a delete issue. I wish to be able to delete a child record by removing it from its parent’s collection and then saving the parent. I have setup a bidirectional one-to-many relationship and inserting/updating is working great.
Here are my mappings
Basket:
<bag name="Items" inverse="true" cascade="all">
<key column="BasketId" />
<one-to-many class="BasketItem" />
</bag>
BasketItem:
<many-to-one not-null="true" name="Basket" column="BasketId" />
I would like to call basket.RemoveBasketItem(BasketItem item)
then Session.SaveUpdate(basket)
so that the basket item will be deleted. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
cascade="all"
更改为cascade="all-delete-orphan"
。cascade="all"
仅当父记录被删除时才会删除您的子记录。Change
cascade="all"
intocascade="all-delete-orphan"
.cascade="all"
will only delete your child records if the parent gets deleted.我有相同的场景,并且在 bagList 中使用了cascade=“all-delete-orphan”,但是当我删除集合中的单个子项时,它也会删除父对象。
I have same scenario and ive used cascade="all-delete-orphan" in bagList but when i delete a single child item in a collection it deletes the parent object as well.
由于从我的集合 getter 返回一个只读的新列表,我遇到了与 initforthemoney 相同的问题。 我发现我可以通过将集合的属性访问策略从
nosetter
更改为field
来继续使用 ReadOnly 列表。I was having the same problem as initforthemoney due to returning a new list as ReadOnly from my collection getter. I found I could continue to use the ReadOnly list by changing the property access strategy of the collection from
nosetter
tofield
.我遇到了一个问题,我的孩子元素返回有序枚举。
我将顺序移至映射并返回子项,就像 IEnumerable 一样。 这对我有用!
I was having a problem where my children elements where returning an Ordered enumerable.
I moved the ordering to my mapping and returned the children as it is for the IEnumerable. This worked for me!