7

我有这个表:

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 ...

有人可以给我一个样品,我该怎么做?- 我的问题是,我必须加入多个 Tables Person -> PersonCompany -> Company。Join Person -> PersonCompany 和 Person -> PersonFavorites 没问题 - 但下一步是 PersonCompany -> Company。

4

1 回答 1

18

映射应类似于以下内容:

实体

    class Person
    {
        ICollection<PersonFavorites> favorites;
        ICollection<PersonCompany> companies;
    }
    
    class PersonFavorite
    {
        Person person;
        Favorite favorite;
    }
    
    class PersonCompany
    {
        Person person;
        Company company;
    }

询问

    Person personAlias = null;
    PersonFavorite personFavoriteAlias = null;
    PersonCompany personCompanyAlias  = null;
    ICollection<PersonCompany> personCompaniesAlias = null;
    Company companyAlias = null;
    Favorite favoriteAlias = null;
    
    var query = Session.QueryOver<Person>(() => personAlias)
        .JoinAlias(() => personAlias.Favorites, () => personFavoriteAlias)
        .JoinAlias(() => personAlias.Companies, () => personCompanyAlias)
        // OR: .JoinAlias(() => personAlias.Companies, () => personCompaniesAlias)
        .JoinAlias(() => personfavoriteAlias.Favourite, () => favoriteAlias)
        .JoinAlias(() => personCompanyAlias.Company, () => companyAlias)
        .Where(() => personCompanyAlias.Person.Name == ...)
        // OR: .Where(() => personCompaniesAlias.Count() == ...)
        // ...
        ;
于 2012-09-29T12:18:06.470 回答