테이블 팀이 있고 테이블 위치가 있습니다. 각 팀은 여러 위치를 가질 수 있으며 각 위치는 여러 팀을 가질 수 있으므로 다 대다 관계입니다.nhibernate - 중간 테이블을 통한 기준
이 시나리오를 데이터베이스에 만들면 many to many 링크를 포함 할 Team, Location 및 TeamLocation 테이블이 3 개 있습니다.
나는 다음과 같은 ... 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
를 사용하여 NHibernate에이를 매핑 한 지금<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>
나는 모든 팀을 검색하는 것을 시도하고있다 어떤 기준을 사용하여 특정 위치를 기반으로하고 있습니다 .........
동등한 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
.. 감사합니다 !
괜찮 았어! –