流畅的 NHibernate 异常在集合之间移动对象
当将对象从一个集合移动到另一个集合并且级联设置为 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 说明:
- 确保项目属性中的连接字符串正确
- 运行项目
- 单击第一个按钮:连接到数据库
- 单击右上角按钮创建表和示例数据(2 个文件夹对象和 1 个文件)
- 单击按钮将文件对象移动到其他文件夹对象
- 单击按钮坚持下去的机会:你会得到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:
- make sure connectionstring in Project's Properties is correct
- run project
- click 1st button: connect to database
- click top right button to create tables and sample data (2 folder objects and 1 file)
- click button to move file object to other folder object
- click button to persist chances: you will get the DeletedObjectException
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.