[英]NHibernate - Associations in memory not updated
I have the following problem model: 我有以下问题模型:
class Item
{
public virtual int Id { get; set; }
public virtual Category Category { get; set;}
// Bi-directional mapping to simplify object traversal.
public virtual Storage Storage { get; set; }
}
class Category
{
public virtual int Id { get; set; }
// Bi-directional mapping to simplify object traversal.
public virtual IList<Item> Items { get; set; }
}
class Storage
{
public virtual int Id { get; set; }
public virtual IList<Item> Items { get; set;}
}
The problem is that whenever I delete a Storage
object containing some Item
instances as follows : 问题是,每当我删除包含一些
Item
实例的Storage
对象时,如下所示:
Category category = ...; // This instance already exists in the database.
Storage storage = ...; // This instance already exists in the database.
Item item = new Item();
item.Category = category; // Inverse is used on HasMany association
category.Items.add(item);
item.Storage = storage; // Inverse is used on HasMany association
storage.Items.add(item);
session.Save(storage);
session.Flush();
// HasMany association on Storage cascades delete to Item
session.Delete(Storage);
session.Flush();
the Item
still remains in Category.Items
. Item
仍保留在Category.Items
。 Is this behavior expected? 这是预期的行为吗? Or is it necessary to explicitly remove the deleted
Item
instances from the parent Category
? 还是有必要从父
Category
明确删除已删除的Item
实例?
I am no more able to find a reference for following statement, but I believe NHibernate considers that removing a deleted entity from loaded one-to-many
collections is not its responsibility. 我再也找不到下面的陈述的参考,但是我相信NHibernate认为从已加载
one-to-many
集合中删除已删除的实体不是其责任。
If you have some remaining work to do on your already loaded category, you should then remove explicitly the deleted items, or Refresh
(see end of 9.2§) the category. 如果要对已加载的类别进行其他工作,则应显式删除已删除的项目,或
Refresh
(请参见9.2§的末尾)类别。
If you have some remaining work to do with the currently opened session, without having anymore knowledge of which Category
are hold in its first level cache with their Items
loaded and containing some deleted ones, and requiring any Category
yielded by this session to have its Items
up to date, you may then Clear
the session before going on. 如果您还有一些剩余的工作要与当前打开的会话相关,而又不知道哪个
Category
保存在其第一级缓存中,则它们的Items
加载并包含一些已删除的项目,并且要求此会话产生的任何Category
都必须具有其Items
您可以先Clear
会话,然后再继续。 (This will evict all loaded entities.) (这将逐出所有已加载的实体。)
But it would be better to simply finish the unit of work at your Flush. 但是最好在您的Flush中完成工作单元。 (Close the session.)
Category
loaded from (or attached into) a newly opened session would have their Items
up to date. (关闭会话。)从新打开的会话加载(或附加到新打开的会话中)的
Category
将使它们的Items
为最新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.