HasMany 在删除时导致 KeyNotFoundException
嗨,我是 nhibernate 的新手,我读了很多有类似问题的线程,但我没有让它工作。 我使用 oracle、nhibernate3 alpha 和 Fluent nhibernate 进行映射。我有父母子女关系。子表有一个复合 ID。选择、插入、更新记录有效。删除没有子记录的父记录有效。但是删除带有子级的父级或仅删除子级会引发 KeyNotFoundException。看来我在映射中遗漏了一些东西?
StackTrace
bei System.Collections.Generic.Dictionary`2.get_Item(TKey key)
bei NHibernate.Engine.StatefulPersistenceContext.RemoveEntity(EntityKey key) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\StatefulPersistenceContext.cs:Zeile 434.
bei NHibernate.Action.EntityDeleteAction.Execute() in d:\CSharp\NH\nhibernate\src\NHibernate\Action\EntityDeleteAction.cs:Zeile 86.
bei NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 130.
bei NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 113.
bei NHibernate.Engine.ActionQueue.ExecuteActions() in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 151.
bei NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) in d:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:Zeile 241.
bei NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) in d:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventListener.cs:Zeile 19.
bei NHibernate.Impl.SessionImpl.Flush() in d:\CSharp\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:Zeile 1524.
bei NHibernate.Transaction.AdoTransaction.Commit() in d:\CSharp\NH\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:Zeile 187.
bei LFF.Kabu.Win.Tabellenverwaltung.DataAccess.NHibernate.UnitOfWork.CommitTransaction() in C:\Demos\Tabellenverwaltung\DataAccess.NHibernate\UnitOfWork.cs:Zeile 77.
bei LFF.Kabu.Win.TabModul.DruckUndVersand.ViewModel.DruckUndVersandVM.SaveData()
我的实体类和映射下面的
public class DruckUndVersand
{
public DruckUndVersand()
{
this.RefFilters = new List<RefDruckUndVersandFilter>();
}
public virtual long Id { get; set; }
public virtual string Programm { get; set; }
public virtual string Variante { get; set; }
public virtual string Beschreibung { get; set; }
public virtual bool IsActive { get; set; }
public virtual IList<RefDruckUndVersandFilter> RefFilters { get; set; }
}
public class RefDruckUndVersandFilter
{
public virtual DruckUndVersand DruckUndVersand { get; set; }
public virtual long Rank { get; set; }
public virtual string Filter { get; set; }
#region override Equals(), GetHashCode()
//
#endregion
}
:我的流畅映射如下所示:
public class DruckUndVersandMapper : ClassMap<DruckUndVersand>
{
public DruckUndVersandMapper()
{
Table("Tab_DruckUndVersand");
Id(x => x.Id, "ID")
.GeneratedBy.Sequence("SEQ_DruckUndVersand");
Map(x => x.Programm).Not.Nullable().Length(255);
Map(x => x.Variante).Length(255);
Map(x => x.Beschreibung).Length(255);
Map(x => x.IsActive).Column("ISACTIVE").CustomType<YesNoType>().Length(1);
HasMany(x => x.RefFilters)
.KeyColumn("IDDruckUndVersand")
.NotFound.Ignore()
.Inverse()
.Cascade.All()
;
}
}
public class RefDruckUndVersandFilterMapper : ClassMap<RefDruckUndVersandFilter>
{
public RefDruckUndVersandFilterMapper()
{
Table("REFDruckUndVersandFILTER");
Not.LazyLoad();
Map(x => x.Filter);
CompositeId()
.KeyReference(x => x.DruckUndVersand, "IDDruckUndVersand")
.KeyProperty(x => x.Rank, "FILTERRANK");
}
}
hi i'm new to nhibernate and i read a lot of threads with similar problems, but i dont get it working.
i use oracle, nhibernate3 alpha and fluent nhibernate for mapping. i have a parent child relation. the child table has a composite id. Select, insert, update records works. Delete a parent without child records works. But deleting a parent with childs or just delete a child throws a KeyNotFoundException. it seems i miss something in my mapping?
StackTrace
bei System.Collections.Generic.Dictionary`2.get_Item(TKey key)
bei NHibernate.Engine.StatefulPersistenceContext.RemoveEntity(EntityKey key) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\StatefulPersistenceContext.cs:Zeile 434.
bei NHibernate.Action.EntityDeleteAction.Execute() in d:\CSharp\NH\nhibernate\src\NHibernate\Action\EntityDeleteAction.cs:Zeile 86.
bei NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 130.
bei NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 113.
bei NHibernate.Engine.ActionQueue.ExecuteActions() in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 151.
bei NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) in d:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:Zeile 241.
bei NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) in d:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventListener.cs:Zeile 19.
bei NHibernate.Impl.SessionImpl.Flush() in d:\CSharp\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:Zeile 1524.
bei NHibernate.Transaction.AdoTransaction.Commit() in d:\CSharp\NH\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:Zeile 187.
bei LFF.Kabu.Win.Tabellenverwaltung.DataAccess.NHibernate.UnitOfWork.CommitTransaction() in C:\Demos\Tabellenverwaltung\DataAccess.NHibernate\UnitOfWork.cs:Zeile 77.
bei LFF.Kabu.Win.TabModul.DruckUndVersand.ViewModel.DruckUndVersandVM.SaveData()
below my entity classes and mappings:
public class DruckUndVersand
{
public DruckUndVersand()
{
this.RefFilters = new List<RefDruckUndVersandFilter>();
}
public virtual long Id { get; set; }
public virtual string Programm { get; set; }
public virtual string Variante { get; set; }
public virtual string Beschreibung { get; set; }
public virtual bool IsActive { get; set; }
public virtual IList<RefDruckUndVersandFilter> RefFilters { get; set; }
}
public class RefDruckUndVersandFilter
{
public virtual DruckUndVersand DruckUndVersand { get; set; }
public virtual long Rank { get; set; }
public virtual string Filter { get; set; }
#region override Equals(), GetHashCode()
//
#endregion
}
my fluent mappings look like this:
public class DruckUndVersandMapper : ClassMap<DruckUndVersand>
{
public DruckUndVersandMapper()
{
Table("Tab_DruckUndVersand");
Id(x => x.Id, "ID")
.GeneratedBy.Sequence("SEQ_DruckUndVersand");
Map(x => x.Programm).Not.Nullable().Length(255);
Map(x => x.Variante).Length(255);
Map(x => x.Beschreibung).Length(255);
Map(x => x.IsActive).Column("ISACTIVE").CustomType<YesNoType>().Length(1);
HasMany(x => x.RefFilters)
.KeyColumn("IDDruckUndVersand")
.NotFound.Ignore()
.Inverse()
.Cascade.All()
;
}
}
public class RefDruckUndVersandFilterMapper : ClassMap<RefDruckUndVersandFilter>
{
public RefDruckUndVersandFilterMapper()
{
Table("REFDruckUndVersandFILTER");
Not.LazyLoad();
Map(x => x.Filter);
CompositeId()
.KeyReference(x => x.DruckUndVersand, "IDDruckUndVersand")
.KeyProperty(x => x.Rank, "FILTERRANK");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在可以使用了。问题是我对 Equals() 和 GetHashCode() 的重写。
i got it working now. the problem was my override for Equals() and GetHashCode().