简体   繁体   中英

Cascade.All() not delete cascade, why?

I need to delete EndPoint members which are mapped on SlotTransceivers and Ports. In Past, EndPoint didn't has own table, this cklass was part of SlotTransceiver and Port.

Problem is that because creating Connection, rhitch has reference to EndPoints, I must had created own table for EndPoints. There were more problems, but now everything works ok, except cascade deleting of EndPoints rows.

I belive that I need write Overriding classes for EndPoint and Connectiions or change this classes for Ports or SlotTransceivers.

Can you give me any dirrection? I'm not author of original classes and I'm noob in Hibernate.

I thought that Cascade.All() means that when I delete any SlotTransceiver or Port, it automatically delete referenced EndPoint.

Isn't there problem that there is possible reference from new table Connection?

There are classes:

public class EndPoint : Entity
    {
        private int _Position;
        private VLAN _VLAN;
        private string _Description;
        private Connection _Connection;

        [Min(0)]
        public virtual int Position
        {
            get
            {
                return _Position;
            }
            set
            {
                _Position = value;
            }
        }

        [NotNull]
        public virtual VLAN VLAN
        {
            get
            {
                return _VLAN;
            }
            set
            {
                _VLAN = value;
            }
        }

        [Length(500)]
        public virtual string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                _Description = value;
            }
        }
    }

    public class SlotTransceiver : Entity
    {
        private EndPoint _EndPoint;
        private SlotTransceiverItem _InType;
        private Slot _Slot;

        [NotNull]
        public virtual EndPoint EndPoint
        {
            get
            {
                return _EndPoint;
            }
            set
            {
                _EndPoint = value;
            }
        }

        [NotNull]
        public virtual SlotTransceiverItem InType
        {
            get
            {
                return _InType;
            }
            set
            {
                _InType = value;
            }
        }

        [NotNull]
        public virtual Slot Slot
        {
            get
            {
                return _Slot;
            }
        }
    }

    public class Port : Item
    { // Item is inherited from Entity
        private EndPoint _EndPoint;
        [NotNull]
        public virtual EndPoint EndPoint
        {
            get
            {
                return _EndPoint;
            }
            set
            {
                _EndPoint = value;
            }
        }
    }

    public class Connection : Entity
    {
        private EndPoint _EndPointIn;
        private EndPoint _EndPointOut;

        [NotNull]
        public virtual EndPoint EndPointIn
        {

            get
            {
                return _EndPointIn;
            }
            set
            {
                _EndPointIn = value;
            }
        }
        [NotNull]
        public virtual EndPoint EndPointOut
        {

            get
            {
                return _EndPointOut;
            }
            set
            {
                _EndPointOut = value;
            }
        }

    }

They are some special mapping:

public class SlotTransceiverOverride : IAutoMappingOverride<SlotTransceiver>
    {
        public void Override(FluentNHibernate.Automapping.AutoMapping<SlotTransceiver> mapping)
        {
            // in past EndPoint columns are in SlotTrancievers resp. in Ports tables
            /*
            mapping.Component(x => x.EndPoint, m =>
            {
                m.Map(x => x.Position);
                m.Map(x => x.Description);
                m.References(x => x.VLAN).ForeignKey("SlotTransceiversEndPointVLANFkConstraint");
            });*/
            mapping.References(x => x.InType).Not.LazyLoad();
            mapping.References(x => x.EndPoint).Not.LazyLoad().Cascade.All();
        }
    }

        public void Override(FluentNHibernate.Automapping.AutoMapping<Port> mapping)
        {
            // in past EndPoint columns are in SlotTrancievers resp. in Ports tables
        /*
            mapping.Component(x => x.EndPoint, m =>
            {

                m.Map(x => x.Position);
                m.Map(x => x.Description);
                m.References(x => x.VLAN).ForeignKey("PortsEndPointVLANFkConstraint");
            });
                 */
            mapping.IgnoreProperty(x => x.AbsoluteIndex);
            mapping.References(x => x.EndPoint).Not.LazyLoad();
            mapping.References(x => x.EndPoint).Cascade.All();
        }

Combining NH attributes and some FNH automapping stuff looks very scary. I do not understand it at all but IMO classic ClassMap is much more transparent. You must pay with more key strokes but your head hurts less:).

public class EndPointMap : ClassMap<EndPoint>
{
    public EndPointMap()
    {
        Id(x => x.Id);
        Map(x => x.Position);
        Map(x => x.Description);
    }
}

public class ConnectionMap : ClassMap<Connection>
{
    public ConnectionMap()
    {
        Id(x => x.Id);

        References(x => x.EndPointIn)
            .Cascade.All()
            .Not.LazyLoad();
        References(x => x.EndPointOut)
            .Cascade.All()
            .Not.LazyLoad();
    }
}

and this code deletes connection and endpoint as well from database:

using (var session = OpenSession())
{
    var connection = session.Get<Connection>(connectionId);
    session.Delete(connection);
    session.Flush();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM