【问题标题】:nhibernate - criteria through intermediate tablenhibernate - 通过中间表的标准
【发布时间】:2012-01-06 16:18:27
【问题描述】:

我有一个桌子团队,我有一个桌子位置。每个团队可以有多个位置,每个位置可以有多个团队,因此是多对多关系。

在数据库中创建此场景时,将有 3 个表,Team、Location 和 TeamLocation 来保存多对多链接。

我已经使用以下方法在 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 

我看不出来:(

纠正我.. 谢谢!

【问题讨论】:

    标签: nhibernate nhibernate-mapping nhibernate-criteria


    【解决方案1】:

    您可以添加restriction to an association path using CreateCriteria or 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>();
    

    【讨论】:

      猜你喜欢
      • 2013-12-22
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多