2012-11-27 135 views
0

試圖將項目添加到iset,但它不會發生。無法將項目添加到iesi.collections.generic.iset

var controlPoint = new ControlPoint {Lon = 53.25253, Lat = 56.23422, Radius = 16}; 
//controlPoint.ApprovedRoutes = new HashedSet<ApprovedRoute>(); 
controlPoint.Save(); 

var route = new ApprovedRoute(); 
route.ControlPoints = new HashedSet<ControlPoint>(); 
route.ControlPoints.Add(controlPoint); 
route.Name = "sometext"; 
route.Save(); 

//controlPoint.ApprovedRoutes.Add(route); 
//controlPoint.Save(); 

類:

public class GeoPoint: Entity<long> 
{ 
    public virtual double Lon { get; set; } 
    public virtual double Lat { get; set; } 
} 

public class ControlPoint: GeoPoint 
{ 
    public virtual double Radius { get; set; } 
    public virtual Iesi.Collections.Generic.ISet<ApprovedRoute> ApprovedRoutes { get; set; } 
} 

public class ApprovedRoute : Entity<long> 
{ 
    public virtual string Name { get; set; } 
    public virtual Iesi.Collections.Generic.ISet<ControlPoint> ControlPoints { get; set; } 
} 

這裏的映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Transport" namespace="Transport.Models"> 
    <class name="GeoPoint" table="GeoPoint"> 
    <id name="Id"> 
     <column name="id" not-null="true" /> 
     <generator class="identity" /> 
    </id> 
    <property name="Lon" /> 
    <property name="Lat" /> 

    <joined-subclass name="ControlPoint" table="ControlPoint"> 
     <key column="Id"/> 
     <property name="Radius"/> 

     <set name="ApprovedRoutes" table="M_ApprovedRoute_ControlPoint" inverse="true"> 
     <key column="PointId"></key> 
     <many-to-many column="RouteId" class="ApprovedRoute" /> 
     </set> 

    </joined-subclass> 
    </class> 
</hibernate-mapping> 

多了一個:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Transport" namespace="Transport.Models"> 
    <class name="ApprovedRoute" table="ApprovedRoute"> 
    <id name="Id"> 
     <column name="id" not-null="true" /> 
     <generator class="identity" /> 
    </id> 
    <property name="Name" /> 
    <set name="ControlPoints" table="M_ApprovedRoute_ControlPoint"> 
     <key column="RouteId"></key> 
     <many-to-many column="PointId" class="ControlPoint" /> 
    </set>  
    </class> 
</hibernate-mapping> 

什麼,我做錯了什麼? ControlPoint和ApprovedRoute記錄出現在表中,但M_ApprovedRoute_ControlPoint仍爲空。

p.s. .Save()調用Session.SaveOrUpdate(obj)

回答

0

這麼容易。我只是將SaveOrUpdate換成了一個事務,現在工作了!

using (ITransaction tx = Session.BeginTransaction()) 
     { 
      try 
      { 
       Session.SaveOrUpdate(obj); 
       Session.Flush(); 
       tx.Commit(); 
      } 
      catch (HibernateException) 
      { 
       tx.Rollback(); 
       throw; 
      } 
     }