views:

27

answers:

1

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

    }
}
A: 

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;
        }

    }
blindmeis