【问题标题】:HasMany cause KeyNotFoundException on DeleteHasMany 在删除时导致 KeyNotFoundException
【发布时间】:2010-10-25 16:00:17
【问题描述】:

嗨,我是 nhibernate 的新手,我读过很多有类似问题的线程,但我没有让它工作。 我使用 oracle、nhibernate3 alpha 和 fluent nhibernate 进行映射。我有亲子关系。子表有一个复合 id。选择、插入、更新记录有效。删除没有子记录的父项有效。但是删除有孩子的父母只删除孩子会引发 KeyNotFoundException。看来我错过了映射中的某些内容?

堆栈跟踪

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");

    }
}

【问题讨论】:

    标签: nhibernate fluent-nhibernate


    【解决方案1】:

    我现在开始工作了。问题是我对 Equals() 和 GetHashCode() 的覆盖。

        public override bool Equals(object obj)
        {
            var toCompare = obj as RefDruckUndVersandFilter;
    
            if (toCompare == null)
                return false;
    
            if (!GetType().Equals(toCompare.GetActualType()))
                return false;
    
            if (ReferenceEquals(this, toCompare))
                return true;
    
            return DruckUndVersand == toCompare.DruckUndVersand
                   && Rank == toCompare.Rank
                   //&& Filter == toCompare.Filter //old causes the error
                   ;
        }
    
        protected virtual Type GetActualType()
        {
            return GetType();
        }
    
        public override int GetHashCode()
        {
            unchecked
            {
                var hashcode = GetType().GetHashCode();
    
                hashcode = (hashcode * 31) ^ (DruckUndVersand != null ? DruckUndVersand.GetHashCode() : 0);
                hashcode = (hashcode * 31) ^ Rank.GetHashCode();
                //hashcode = (hashcode * 31) ^ (Filter!= null ? Filter.GetHashCode() : 0); old
    
                return hashcode;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多