NHibernate:删除的对象将被级联重新保存。替换对象并删除旧的 [英] NHibernate: deleted object would be re-saved by cascade. Replace object and remove old one
问题描述
我试图在ProgramItem的TimeBlock对象上进行替换,然后删除旧的TimeBlock对象。这是删除部分,给我的问题。
我有一些'相对的'简单nHibernate probelms删除旧的TimeBlock对象。
例外:
删除的对象将被级联重新保存(从关联中删除删除的对象)[* .ModelTimeBlock#15]
15是oldTimeBlock的Id p>
我一般都没有任何问题来解决这种问题,我只是尝试了任何用法:
oldTimeBlock.ProgramItems = new List< ProgramItem>();
programItem.TimeBlock = null;
以及任何顺序的保存和删除块,并用Inverse() >
我需要一些新鲜的眼睛 - 我怎么能做到这一点?
$ b 代码:
public class TimeBlock
{
public virtual int Id {get;组; }
公共虚拟IList< ProgramItem> ProgramItems {get;组; }
...
}
public class TimeBlockMap:ClassMap< TimeBlock>
{
public TimeBlockMap()
{
Id(x => x.Id);
HasMany(x => x.ProgramItems).Cascade.SaveUpdate(); //已经使用Inverse()进行了测试,但似乎没有任何区别
}
}
public class ProgramItem:ImageModel,IIdentifiable
{
public virtual int Id {get;组; }
public virtual TimeBlock TimeBlock {get;组; }
...
}
public class ProgramItemMap:ClassMap< ProgramItem>
{
public ProgramItemMap()
{
Id(x => x.Id);
参考文献(x => x.TimeBlock);
}
}
//删除旧的TimeBlock并将新的TimeBlock设置为ProgramItem
var oldTimeBlock = programItem.TimeBlock;
using(var tx = session.BeginTransaction())
{
oldTimeBlock.ProgramItems = new List< ProgramItem>();
programItem.TimeBlock = null;
//session.Save(programItem);
//session.Save (oldTimeBlock);
session.Delete(oldTimeBlock);
tx.Commit(); //异常的位置如果我移动save programItem部分下面的删除oldTimeBlock部分,它仍然会在删除oldTimeBlock部分失败。
using(var tx = session.BeginTransaction())
{
programItem.TimeBlock = timeBlock;
session.Save(programItem);
tx.Commit();
在这里详细
我会说,重点在这里: ProgramItem
被其他一些集合引用。那个集合也加载到会话中。
开始的最佳位置是 - 无论使用哪个ProgramItem(集合项目,引用),都将映射更改为 Cascade.None() code>。然后尝试执行您的删除代码。这将工作...当然,因为现在级联...没有办法如何触发这种异常。
下一步 - 开始级联。确保你知道什么是允许的,然后你将知道哪些引用,集合重新保存你的对象。
我自己的经验/方法是 - 如果你删除项目从一个地方,明确地从其他人删除,如果有级联。这可能是具有挑战性的,但... ...
I am trying to replace at TimeBlock object on a ProgramItem and then delete the old TimeBlock object. It is the delete part that gives me problems. I have some 'relative' simple nHibernate probelms deleting the old TimeBlock object.
Exception:
deleted object would be re-saved by cascade (remove deleted object from associations)[*.Model.TimeBlock#15]
15 is the Id of the oldTimeBlock
I usaully doesn't have any problems solving this kind of issue by I just tried amost any usages of:
oldTimeBlock.ProgramItems = new List<ProgramItem>();
programItem.TimeBlock = null;
and the save and delete blocks in any order and changing the mapping with Inverse().
I need some fresh eyes - how could I make this work?
Code:
public class TimeBlock
{
public virtual int Id { get; set; }
public virtual IList<ProgramItem> ProgramItems { get; set; }
...
}
public class TimeBlockMap : ClassMap<TimeBlock>
{
public TimeBlockMap()
{
Id(x => x.Id);
HasMany(x => x.ProgramItems).Cascade.SaveUpdate(); // Have tested with Inverse() but seemed to make no difference
}
}
public class ProgramItem : ImageModel, IIdentifiable
{
public virtual int Id { get; set; }
public virtual TimeBlock TimeBlock { get; set; }
...
}
public class ProgramItemMap : ClassMap<ProgramItem>
{
public ProgramItemMap()
{
Id(x => x.Id);
References(x => x.TimeBlock);
}
}
//Delete old TimeBlock and set new TimeBlock to ProgramItem
var oldTimeBlock = programItem.TimeBlock;
using (var tx = session.BeginTransaction())
{
oldTimeBlock.ProgramItems = new List<ProgramItem>();
programItem.TimeBlock = null;
//session.Save(programItem);
//session.Save(oldTimeBlock);
session.Delete(oldTimeBlock);
tx.Commit(); // location of the exception. If i move the delete oldTimeBlock part below the save programItem part it will still fail in the delete oldTimeBlock part.
}
using (var tx = session.BeginTransaction())
{
programItem.TimeBlock = timeBlock;
session.Save(programItem);
tx.Commit();
}
I tried explain what is happening in detail here
- NHibernate Deleted object would be re-saved by cascade
- Delete an item from many-to-many relationship
I would say, that the point is here: the ProgramItem
is referenced by some other collection. And that collection was also loaded into the session.
The best place where to start is - wherever is ProgramItem used (collection item, reference) change the mapping to Cascade.None()
. Then try to execute your deletion code. That will work... surely, becuase now cascading in place... no way how to trigger that exception.
Next - start cascading by pieces. Be sure you know what is allowed, and then you will know which reference, collection is re-saving your object.
My own experience/approach is - if you delete the item from one place, explicitly delete it from others as well, if there is cascade. It could be challenging, but...
这篇关于NHibernate:删除的对象将被级联重新保存。替换对象并删除旧的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!