2010-12-27 2 views
1

У меня есть запросNHibernate JoinQueryOver и Ленивый Loading

var query = _session.QueryOver<TEntity>().JoinQueryOver<PropertyMultTable>(p => p.Properties).Where(propertyPredicate).List(); 

этот запрос создаст следующий SQL

select this_.BaseEntity_id as BaseId0_1_ , 
     this_1_.Label as Label0_1_ , 
     this_1_.Description as Descript3_0_1_ , 
     this_1_.CreatedDate as CreatedD4_0_1_ , 
     this_.Width as Width2_1_ , 
     this_.Height as Height2_1_ , 
     this_.Duration as Duration2_1_ , 
     propertymu1_.id as id4_0_ , 
     propertymu1_.Name as Name4_0_ , 
     propertymu1_1_.DateTimeValue as DateTime2_5_0_ , 
     propertymu1_2_.IntegerValue as IntegerV2_6_0_ , 
     propertymu1_3_.DecimalValue as DecimalV2_7_0_ , 
     propertymu1_4_.StringValue as StringVa2_8_0_ 
from [Video] this_ 
     inner join [BaseEntity] this_1_ on this_.BaseEntity_id = this_1_.BaseId 
     inner join [PropertyMultTable] propertymu1_ on this_.BaseEntity_id = propertymu1_.BaseEntity_id 
     left outer join DateTimeValues propertymu1_1_ on propertymu1_.id = propertymu1_1_.PropertyMultTable_id 
     left outer join IntegerValues propertymu1_2_ on propertymu1_.id = propertymu1_2_.PropertyMultTable_id 
     left outer join DecimalValues propertymu1_3_ on propertymu1_.id = propertymu1_3_.PropertyMultTable_id 
     left outer join StringValues propertymu1_4_ on propertymu1_.id = propertymu1_4_.PropertyMultTable_id 
where (propertymu1_2_.IntegerValue >= 459144 
      and propertymu1_2_.IntegerValue <= 691982 
     ) 

, но я хочу, чтобы получить только лицо, без свойств. Таким образом, мне нужно SQL, как это:

select this_.BaseEntity_id as BaseId0_1_ , 
     this_1_.Label as Label0_1_ , 
     this_1_.Description as Descript3_0_1_ , 
     this_1_.CreatedDate as CreatedD4_0_1_ , 
     this_.Width as Width2_1_ , 
     this_.Height as Height2_1_ , 
     this_.Duration as Duration2_1_ 
from [Video] this_ 
     inner join [BaseEntity] this_1_ on this_.BaseEntity_id = this_1_.BaseId 
     inner join [PropertyMultTable] propertymu1_ on this_.BaseEntity_id = propertymu1_.BaseEntity_id 
     left outer join DateTimeValues propertymu1_1_ on propertymu1_.id = propertymu1_1_.PropertyMultTable_id 
     left outer join IntegerValues propertymu1_2_ on propertymu1_.id = propertymu1_2_.PropertyMultTable_id 
     left outer join DecimalValues propertymu1_3_ on propertymu1_.id = propertymu1_3_.PropertyMultTable_id 
     left outer join StringValues propertymu1_4_ on propertymu1_.id = propertymu1_4_.PropertyMultTable_id 
where (propertymu1_2_.IntegerValue >= 459144 
      and propertymu1_2_.IntegerValue <= 691982 
     ) 

или даже намного лучше, как это:

select distinct this_.BaseEntity_id as BaseId0_1_ , 
     this_1_.Label as Label0_1_ , 
     this_1_.Description as Descript3_0_1_ , 
     this_1_.CreatedDate as CreatedD4_0_1_ , 
     this_.Width as Width2_1_ , 
     this_.Height as Height2_1_ , 
     this_.Duration as Duration2_1_ 
from [Video] this_ 
     inner join [BaseEntity] this_1_ on this_.BaseEntity_id = this_1_.BaseId 
     inner join [PropertyMultTable] propertymu1_ on this_.BaseEntity_id = propertymu1_.BaseEntity_id 
     left outer join IntegerValues propertymu1_2_ on propertymu1_.id = propertymu1_2_.PropertyMultTable_id 
where (propertymu1_2_.IntegerValue >= 459144 
      and propertymu1_2_.IntegerValue <= 691982 
     ) 

Могу ли я сделать это с Fluent NHibernate? Спасибо за ответы.

+0

Вы можете добавить класс декларацию видео? – Baz1nga

+0

Видео - это класс с 3 свойствами Ширина, Высота и Длительность, и он получен из BaseEntity, который содержит только свойства: Id, Label, Description and CreatedDate – msi

+0

Fluent NHibernate используется для создания сопоставлений, а не для создания запросов , –

ответ

0

вот как вы можете это сделать, используя HQL.

var hqlQuery=string.Format("select v from Video as v inner join v.BaseEntity 
as be inner join v.PropertyMultTable as pmt left outer join pmt.IntegerValues 
as iv where be.BaseEntity_id=v.BaseId and be.BaseEntity_id=pmt.BaseEntity_id and 
pmt.Id=iv.PropertyMultTable_id and iv.IntegerValue >={0} and iv.IntegerValue 
<={1}", 459144,691982); 

примечание здесь, когда выполняется внутреннее соединение. Предполагается, что имена свойств такие же, как указано в запросе выше. они должны быть точно такими же, как упоминание вашей собственности, или вы получите исключение nhibernate.

Если он не работает, разместите всю диаграмму классов.

вы можете запустить этот запрос следующим образом:

session.CreateQuery(hqlquery).List<Video>(); 
+0

Это не решение моей цели. Мне не нужен HQL, потому что у меня есть общий репозиторий, который может принимать как TEntity разные классы, такие как Image или Document. – msi

+0

Тогда вам следует, вероятно, рассмотреть запросы Criteria. Я могу помочь вам, если вы предоставите мне диаграммы классов. – Baz1nga

0

Может быть, с помощью Future():

var query = 
    _session.QueryOver<TEntity>() 
    .JoinQueryOver<PropertyMultTable>(p => p.Properties) 
    .Future() 
    .Where(propertyPredicate).List(); 
Смежные вопросы