HasMany 在删除时导致 KeyNotFoundException

发布于 2024-09-28 21:31:47 字数 3825 浏览 2 评论 0原文

嗨,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2024-10-05 21:31:47

我现在可以使用了。问题是我对 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;
        }

    }

i got it working now. the problem was my override for Equals() and 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;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文