0

テーブルチームがあり、テーブルロケーションがあります。各チームは複数の場所を持つことができ、各場所は複数のチームを持つことができるため、多対多の関係になります。

このシナリオがデータベースに作成されると、多対多のリンクを保持するために、Team、Location、および TeamLocation の 3 つのテーブルが作成されます。

次を使用してこれを nhibernate にマップしました... Team.hbm.xml

<class name="App.Data.Entities.Team,App.Data.Entities" table="`Team`" lazy="true">
   <id name="Teamid" column="`TeamID`" type="Guid">
      <generator class="assigned" />
    </id>
    <property type="string" not-null="true" length="250" name="TeamName" column="`TeamName`" />
    <bag name="TeamLocations" inverse="false" table="`TeamLocations`" lazy="true" cascade="all">
      <key column="`TeamID`" />
      <many-to-many class="App.Data.Entities.Location,App.Data.Entities">
        <column name="`LocationID`" />
      </many-to-many>
    </bag>
    </class>

および Location.hbm.xml

<class name="App.Data.Entities.Location,App.Data.Entities" table="`Location`" lazy="true">
    <id name="Locationid" column="`LocationID`" type="int">
      <generator class="native" />
    </id>
    <property type="string" not-null="true" length="250" name="LocationName" column="`LocationName`" />
    <property type="string" length="1000" name="Address" column="`Address`" />
    <bag name="Teams" inverse="false" table="`TeamLocations`" lazy="true" cascade="all">
      <key column="`LocationID`" />
      <many-to-many class="App.Data.Entities.Team,App.Data.Entities">
        <column name="`TeamID`" />
      </many-to-many>
    </bag>
  </class>

現在、基準 API を使用して特定の場所に拠点を置くすべてのチームを取得しようとしていますが、これはできません..

同等のSQL...

SELECT     Team.TeamName, Location.LocationName
FROM         Team INNER JOIN
                      TeamLocations ON Team.TeamID = TeamLocations.TeamID INNER JOIN
                      Location ON TeamLocations.LocationID = Location.LocationID
WHERE     (TeamLocations.LocationID = 2) 

そしてasp.netコードで

ICriteria criteria = _session.CreateCriteria(typeof(Team)).SetFirstResult(startRowIndex).SetMaxResults(maximumRows);

if (deptID > 0)
{
    Department dept = new Department(deptID);
    criteria.Add(Expression.Eq(Team.PropertyNames.Department, dept)); //for department, which works fine.
}

if (locationIDs.Count > 0)
{
    List<Location> locations = new List<Location>();

    foreach (int locationID in locationIDs)
    {
        Location loc = new Location(locationID);
        locations.Add(loc);
    }

    criteria.CreateCriteria("TeamLocations").Add(Expression.Eq(""TeamLocations"", locations));
}

return criteria.List<Team>();

これはスローされ、エラーが発生します..

could not resolve property: TeamLocations of: App.Data.Entities.Location 

私は理解することができません:(

私を訂正してください..ありがとう!

4

1 に答える 1

1

以下で行ったように、CreateCriteria または CreateAlias を使用して関連付けパスに制限を追加できます。エイリアスを作成せずに Team.PropertyNames.Department に制限を追加できる理由を理解するには、モデルとマッピングについて詳しく知る必要があります。

    ICriteria criteria = _session.CreateCriteria(typeof(Team)).SetFirstResult(startRowIndex).SetMaxResults(maximumRows);

if (deptID > 0)
{
    Department dept = new Department(deptID);
    criteria.Add(Expression.Eq(Team.PropertyNames.Department, dept)); //for department, which works fine.
}

if (locationIDs.Count > 0)
{
    // you may need to convert locationIds to an ICollection e.g.
    // var ids = locationIds.ToArray();
    criteria.CreateAlias("TeamLocations", "teamLocations");
    criteria.Add(Restrictions.In("teamLocations.LocationId", locationIds));
}

return criteria.List<Team>();
于 2012-01-06T16:52:01.590 に答える