0

項目を 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>

もう1つ:

<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 はまだ空です。

ps .Save() は Session.SaveOrUpdate(obj) を呼び出します

4

1 に答える 1

0

とても簡単。SaveOrUpdate をトランザクションでラップするだけで、これで問題なく動作します。

using (ITransaction tx = Session.BeginTransaction())
        {
            try
            {
                Session.SaveOrUpdate(obj);
                Session.Flush();
                tx.Commit();
            }
            catch (HibernateException)
            {
                tx.Rollback();
                throw;
            }
        }
于 2012-11-27T19:38:12.517 に答える