在 nhibernate 中,无法更新子对象列表

发布于 2024-10-07 02:36:43 字数 1186 浏览 2 评论 0 原文

我有 x ,它是一个 objectX 类型的对象,它有一个属性 ListOfObjectYs

List<objectY>

nhibernate 映射看起来像这样:

    public ObjectXMap()
    {
        HasMany(x => x.ListOfObjectYs).AsBag().Inverse(); 
    }

当我去保存它时,我更改了 objectX 上的一些属性,然后:

    Session.SaveOrUpdate(x);

现在我需要更新这个列表属性。我得到一个新的 objectY 列表,我想用新列表替换现有的 objectY 列表。我需要这样做吗?

  foreach (ObjectY y in x.ListOfObjectYs)
                {
                   Session.Delete(y);
                    deleted = true;
                }
                if (deleted)
                {
                   _session.Flush();
                }
                x.ListOfObjectYs.Clear();

                foreach (ObjectY y in newObjectYList)
                {
                    x.ListOfObjectYs.Add(y);
                   Session.SaveOrUpdate(y);
                }
                _session.Flush();

我的问题是:

  1. 在添加新的之前我是否必须删除所有内容并刷新。
  2. 我是否需要在两者之间进行所有这些增量保存,

是否有更好的方法来执行此更新,我需要更新对象(属性),但也更新列表中存在全新列表的属性(这意味着项目需要待删除和添加)。

i have x which an object of type objectX which has a property ListOfObjectYs thats a

List<objectY>

the nhibernate mapping looks like this:

    public ObjectXMap()
    {
        HasMany(x => x.ListOfObjectYs).AsBag().Inverse(); 
    }

when i go to save it, i change some properties on objectX and then have:

    Session.SaveOrUpdate(x);

now i need to update this property that is a list. I get a new list of objectYs and i want to replace the existing list of objectY with a new list. do i need to do this?

  foreach (ObjectY y in x.ListOfObjectYs)
                {
                   Session.Delete(y);
                    deleted = true;
                }
                if (deleted)
                {
                   _session.Flush();
                }
                x.ListOfObjectYs.Clear();

                foreach (ObjectY y in newObjectYList)
                {
                    x.ListOfObjectYs.Add(y);
                   Session.SaveOrUpdate(y);
                }
                _session.Flush();

my questions are:

  1. Do i have to delete everything and flush before adding new ones.
  2. Do i need to do all of these incremental saves in between

is there a better way of doing this update where i need to update an object (properties) but also update properties that are list where there is a whole new list (meaning that items need to be deleted and added).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2024-10-14 02:36:43

你的问题的答案是否定的。如果您想替换列表,您应该清除它并添加新项目。如果您将级联设置为 all-delete-orphan,如 James Kovacs 的回答所示,那么当会话刷新时,对集合的更改将被保留。

了解 NHibernate 中 Save、Update 和 SaveOrUpdate 的含义非常重要:

  • Save - 使新对象持久化
  • Update - 使更改的分离对象持久化
  • SaveOrUpdate - 根据对象标识符的未保存值保存或更新

另请参阅 操作持久数据

假设所有对象都在同一个会话中加载,那么替换集合可能就像以下一样简单:

x.ListOfObjectYs.Clear();
foreach (ObjectY y in newObjectYList)
{
    x.ListOfObjectYs.Add(y);
}
_session.Flush();

The answers to your questions are no and no. If you want to replace the list you should clear it and add new items. If you have cascade set to all-delete-orphan, as in James Kovacs' answer, then the changes to the collection will be persisted when the session is flushed.

It's important to understand what Save, Update, and SaveOrUpdate mean in NHibernate:

  • Save - make a new object persistent
  • Update - make a changed detached object persistent
  • SaveOrUpdate - Save or Update depending on the unsaved-value of the object's identifier

See also Manipulating Persistent Data.

Assuming that all your objects were loaded in the same session then replacing a collection may be as easy as:

x.ListOfObjectYs.Clear();
foreach (ObjectY y in newObjectYList)
{
    x.ListOfObjectYs.Add(y);
}
_session.Flush();
颜漓半夏 2024-10-14 02:36:43

您需要在 HasMany() 上进行级联。如果子对象完全由父对象拥有,Cascade.AllDeleteOrphan() 是一个不错的选择。这样,父对象上的保存、更新和删除会自动级联到子对象,从而无需复杂的 foreach 块。完成此操作后,只需更新您的集合并提交您的交易即可。 NHibernate 将处理剩下的事情。

更新:要修改列表,只需在列表中添加和删除项目,就像通常使用 .NET 集合一样。例如:

public void RemoveY(ObjectY someY) {
    ListOfObjectYs.Remove(someY);
}

public void AddY(ObjectY someY) {
    ListOfObjectYs.Add(someY);
}

public void ClearAllY() {
    ListOfObjectYs.Clear();
}

提交事务时,对 ListOfObjectYs 集合的任何更改都将与父 ObjectX 一起保留。

You need a cascade on your HasMany(). If the child objects are fully owned by the parent, Cascade.AllDeleteOrphan() is a good choice. This way saves, updates, and deletes on the parent are automatically cascaded to the child objects, which removes the need for your complex foreach block. With this in place, simply update your collection and commit your transaction. NHibernate will take care of the rest.

UPDATE: To modify the list, simply add and remove items from the list as you normally would with a .NET collection. For example:

public void RemoveY(ObjectY someY) {
    ListOfObjectYs.Remove(someY);
}

public void AddY(ObjectY someY) {
    ListOfObjectYs.Add(someY);
}

public void ClearAllY() {
    ListOfObjectYs.Clear();
}

When the transaction is committed, any changes to the ListOfObjectYs collection will be persisted along with the parent ObjectX.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文