2011-04-22 52 views
2

我需要刪除映射到SlotTransceivers和端口的EndPoint成員。在過去,EndPoint沒有自己的表,這個cklass是SlotTransceiver和Port的一部分。Cascade.All()不刪除級聯,爲什麼?

問題是因爲創建Connection,rhitch引用EndPoints,所以我必須爲EndPoints創建自己的表。還有更多的問題,但現在一切正常,除了級聯刪除EndPoints行。

我相信我需要編寫爲EndPoint和Connectiions重寫類或爲Ports或SlotTransceivers更改此類。

你可以給我任何dirrection?我不是原創類的作者,我在Hibernate中是noob。

我以爲Cascade.All()意味着當我刪除任何SlotTransceiver或端口時,它會自動刪除引用的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(); 
     } 

回答

0

結合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(); 
} 
+0

謝謝回答。我認爲,不僅FNH測繪,但所有NH看起來非常可怕。我也不明白,但似乎它應該做什麼。有時... – Zdenek 2011-05-10 07:21:52