nhibernate - 通过中间表的标准
我有一个团队表和一个位置表。每个团队可以有多个位置,每个位置可以有多个团队,因此这是多对多的关系。
当在数据库中创建此场景时,将有 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
我无法弄清楚:(
纠正我.. 谢谢!
I have a table Team and I have a table Location. Each team can have multiple locations and each location could have multiple teams, so it is a many to many relationship.
When this scenario is created in the database, there will be 3 tables, Team, Location and TeamLocation to hold the many to many links.
I have mapped this in nhibernate using the following...
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>
and 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>
Now I am trying to retrieve all teams which are based in a specific location using criteria API, which I am unable to..
Equivalent 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)
and in asp.net code
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>();
This throws and error..
could not resolve property: TeamLocations of: App.Data.Entities.Location
which I cant seen to figure out :(
Correct me..
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加 使用 CreateCriteria 或 CreateAlias 限制关联路径,如下所示。我必须更多地了解您的模型和映射,才能了解为什么您可以在 Team.PropertyNames.Department 上添加限制而不使用别名。
You can add a restriction to an association path using CreateCriteria or CreateAlias as I did below. I would have to know more about your model and mapping to understand why you can add a restriction on Team.PropertyNames.Department without aliasing it.