使用NHibernate和QueryOver连接多个表

时间:2012-09-29 10:50:24

标签: nhibernate queryover nhibernate-3

我有这张表:

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的下一步 - >公司。

1 个答案:

答案 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)