我有这张表:
Person -> PersonFavorites, PersonCompany
PersonCompany -> Company
我现在要用NHibernate和QueryOver进行以下选择:
select * from Person
inner join PersonFavorites on Person.Id = PersonFavorites.PersonId
inner join PersonCompany on Person.Id = PersonCompany.PersonId
inner join Company on Company.Id = PersonCompany.CompanyId
where ...
有人可以给我一个样品,我该怎么做? - 我的问题是,我必须加入多个表格人员 - > PersonCompany - >公司。加入人 - > PersonCompany and Person - > PersonFavorites没问题 - 但是PersonCompany的下一步 - >公司。
答案 0 :(得分:10)
映射应类似于以下内容:
<强>实体强>
class Person
{
Collection<PersonFavorites> favorites;
Collection<PersonCompany> companies;
}
class PersonFavorite
{
Person person;
Favorite favorite;
}
class PersonCompany
{
Person person;
Company company;
}
<强>查询强>
Person personAlias = null;
PersonFavorites personfavouriteAlias = null;
PersonCompany personCompanyAlias = null;
Company companyAlias = null;
Favorite favouriteAlias = null;
var query = Session.QueryOver<Person>(() => personAlias)
.joinAlias(() => personAlias.favorites, () => personfavouriteAlias)
.joinAlias(() => personAlias.companies, () => personCompanyAlias)
.joinAlias(() => personfavouriteAlias.Favourite, () => favouriteAlias)
.joinAlias(() => personCompanyAlias.Company, () => companyAlias)