本文介绍了使用NHibernate和QueryOver联接多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有这张桌子:

Person -> PersonFavorites, PersonCompany
PersonCompany -> Company

我现在必须使用NHibernate和QueryOver进行以下选择:

I have now to do the following select with NHibernate and 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 ...

有人可以给我样品吗,我该怎么办? -我的问题是,我必须加入多个表Person-> PersonCompany-> Company.加入Person-> PersonCompany和Person-> PersonFavorites没问题-但这是PersonCompany-> Company的下一步.

Can someone give me a sample, how I can do that? - My Problem is, that I have to join multiple Tables Person -> PersonCompany -> Company. The Join Person -> PersonCompany and Person -> PersonFavorites are no problem - but the next step from PersonCompany -> Company.

推荐答案

映射应类似于以下内容:

Mapping should be similar to the following:

实体

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)

这篇关于使用NHibernate和QueryOver联接多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 18:56