加入与NHibernate和QueryOver多个表(Join multiple tables wi

2019-09-23 04:45发布

我有这个表:

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和Person - > PersonFavorites都没有问题 - 但是从PersonCompany下一步 - >公司。

Answer 1:

映射应类似于以下内容:

实体

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)


文章来源: Join multiple tables with NHibernate and QueryOver