流畅的 NHibernate 异常在集合之间移动对象

发布于 2024-11-09 14:47:38 字数 1508 浏览 0 评论 0原文

当将对象从一个集合移动到另一个集合并且级联设置为 all-delete-orphan 时,出现以下异常:

已删除的对象将由级联重新保存(从关联中删除已删除的对象)

我认为 nhibernate 不会删除使用 all-delete-orphan 时在另一个集合中引用该对象。

任何人都可以确认,当您有像文件夹这样包含文件夹或文件的对象,并且您将文件从一个文件夹移动到另一个文件夹时,您不应该得到此异常吗?

我在 vs2010 中制作了一个示例项目来演示这种行为。谁能说我的映射是否正确或者 nhibernate 中是否存在错误?

FileMapping.cs

public class FileMapping: ClassMap<File>
{
    public FileMapping()
    {
        Id(x => x.Id, "Id").GeneratedBy.Native("File_seq");
        Map(x => x.Name, "Name").Not.Nullable();
        References(x => x.Folder).Not.Nullable().Column("idFolder");
    }
}

FolderMapping.cs

public class FolderMapping: ClassMap<Folder>
{
    public FolderMapping()
    {
        Id(x => x.Id, "Id").GeneratedBy.Native("Folder_seq");
        Map(x => x.Name, "Name").Not.Nullable();
        HasMany(x => x.Folders).Inverse().Cascade.AllDeleteOrphan().KeyColumn("idParentFolder");
        HasMany(x => x.Files).Inverse().Cascade.AllDeleteOrphan().KeyColumn("idFolder");
        References(x => x.ParentFolder).Nullable().Column("idParentFolder");
    }
}

示例项目: http://www.mediafire.com/ ?orxcw63aziq54xo 说明:

  1. 确保项目属性中的连接字符串正确
  2. 运行项目
  3. 单击第一个按钮:连接到数据库
  4. 单击右上角按钮创建表和示例数据(2 个文件夹对象和 1 个文件)
  5. 单击按钮将文件对象移动到其他文件夹对象
  6. 单击按钮坚持下去的机会:你会得到DeletedObjectException

When moving an object from one collection to another and when cascade is set to all-delete-orphan, I get the following exception:

deleted object would be re-saved by cascade (remove deleted object from associations)

I thought that nhibernate would not delete an object when it is referenced in another collection when you use all-delete-orphan.

Can anyone confirm that, when you have objects like Folders which contain Folders or Files and you move a File from one Folder to another, you should not get this exception?

I made a sample project in vs2010 which demonstrates this behavior. Can anyone say if my mappings are correct or if there is a bug in nhibernate?

FileMapping.cs

public class FileMapping: ClassMap<File>
{
    public FileMapping()
    {
        Id(x => x.Id, "Id").GeneratedBy.Native("File_seq");
        Map(x => x.Name, "Name").Not.Nullable();
        References(x => x.Folder).Not.Nullable().Column("idFolder");
    }
}

FolderMapping.cs

public class FolderMapping: ClassMap<Folder>
{
    public FolderMapping()
    {
        Id(x => x.Id, "Id").GeneratedBy.Native("Folder_seq");
        Map(x => x.Name, "Name").Not.Nullable();
        HasMany(x => x.Folders).Inverse().Cascade.AllDeleteOrphan().KeyColumn("idParentFolder");
        HasMany(x => x.Files).Inverse().Cascade.AllDeleteOrphan().KeyColumn("idFolder");
        References(x => x.ParentFolder).Nullable().Column("idParentFolder");
    }
}

Sample project: http://www.mediafire.com/?orxcw63aziq54xo
Instructions:

  1. make sure connectionstring in Project's Properties is correct
  2. run project
  3. click 1st button: connect to database
  4. click top right button to create tables and sample data (2 folder objects and 1 file)
  5. click button to move file object to other folder object
  6. click button to persist chances: you will get the DeletedObjectException

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

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

发布评论

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

评论(1

慕烟庭风 2024-11-16 14:47:38

NHibernate 对孤儿有非常本地化的看法。如果将某个对象从文件夹 A 移动到文件夹 B,则文件夹 A 会将其视为孤立对象,因此将其删除。文件夹 B 想要更新对象,发生冲突。

这称为重新育儿,您可以在此处阅读http:// /fabiomaulo.blogspot.com/2009/09/nhibernate-tree-re-parenting.html

基本上,这是一个重新定义“孤儿”在您的集合中的含义的选项,以便您的对象不会被删除。

NHibernate has a very local view on orphans. If an object is moved from folder A to folder B folder A considers it an orphan and therefore deletes it. Folder B wants to update the object and a conflict occurs.

It is called re-parenting and you read about it here http://fabiomaulo.blogspot.com/2009/09/nhibernate-tree-re-parenting.html

Basically this is a option to redefine what Orphan means in your collection so your objects don't get deleted.

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