HasMany导致Delete上的KeyNotFoundException

时间:2010-10-22 08:28:52

标签: nhibernate fluent-nhibernate

嗨,我是nhibernate的新手,我读了很多类似问题的线程,但是我没有让它工作。 我使用oracle,nhibernate3 alpha和流畅的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");

    }
}

1 个答案:

答案 0 :(得分:0)

我现在就开始工作了。问题是我对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;
        }

    }