Cascade.All() 没有删除级联,为什么?

发布于 2024-11-02 13:36:20 字数 4569 浏览 2 评论 0原文

我需要删除映射到 SlotTransceivers 和端口上的 EndPoint 成员。过去,EndPoint 没有自己的表,这个时钟类是 SlotTransceiver 和 Port 的一部分。

问题是,因为创建连接,rhitch 引用了端点,所以我必须为端点创建自己的表。还有更多问题,但现在一切正常,除了 EndPoints 行的级联删除之外。

我相信我需要为 EndPoint 和 Connectioniions 编写重写类,或者更改端口或 SlotTransceivers 的此类。

你能给我任何指示吗?我不是原始类的作者,而且我对 Hibernate 也很菜鸟。

我认为Cascade.All()意味着当我删除任何SlotTransceiver或Port时,它会自动删除引用的EndPoint。

是否存在新表连接可能引用的问题?

有一些类:

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

    }

它们是一些特殊的映射:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不一样的天空 2024-11-09 13:36:20

将 NH 属性和一些 FNH 自动映射内容结合起来看起来非常可怕。我根本不明白,但 IMO 经典的 ClassMap 更加透明。你必须用更多的击键来支付,但你的头疼得更少:)。

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

此代码还从数据库中删除连接和端点:

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

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