Linq - wrong SQL generated when selecting subclasses described with discriminators
Description
Environment
Attachments
Activity

Anton June 18, 2012 at 4:02 PM
Yes, but we should use it to achieve lazy behavior, otherwise it will be impact on performance

Alex Zaytsev June 18, 2012 at 3:49 PM
Problem there that you are using fetch="select" on joins.

Anton June 18, 2012 at 1:52 PM
Alexander, Animal.hbm.xml - it is different mapping, which uses joined-subclass instead of subclass with discriminator + join.

Анатолий Иванов June 18, 2012 at 11:55 AM
We can get the correct results using joined-subclass (see Animal.hbm.xml mapping file) but in this case there are a lot of left outer joins to all the subclasses (that could impact the performance):
select
children1_3_.Pregnant as col_0_0_
from
t_animal animal0_
inner join
t_animal children1_
on animal0_.Id=children1_.ParentId
left outer join
t_reptile children1_1_
on children1_.Id=children1_1_.Id
left outer join
t_lizard children1_2_
on children1_.Id=children1_2_.Id
left outer join
t_mammal children1_3_
on children1_.Id=children1_3_.Id
left outer join
t_dog children1_4_
on children1_.Id=children1_4_.Id
left outer join
t_cat children1_5_
on children1_.Id=children1_5_.Id
where
case
when children1_2_.Id is not null then 2
when children1_4_.Id is not null then 4
when children1_5_.Id is not null then 5
when children1_1_.Id is not null then 1
when children1_3_.Id is not null then 3
when children1_.Id is not null then 0
end=3
Could it be possible to generate such SQL-query in case of using subclasses with discriminators if it is impossible to get SQL like (see the issue description):
select m._Pregnant, ...
from t_animal animal0_
inner join t_animal children1_ on animal0_.Id=children1_.ParentId
inner join t_mammal m
on children1_.Id=m.Id
where children1_.ANIMAL_TYPE='MAMMAL'

Alex Zaytsev June 18, 2012 at 11:45 AM
Anatoly, is your last attachement differents to one in the patch?
Details
Assignee
UnassignedUnassignedReporter
Анатолий ИвановАнатолий ИвановLabels
Components
Affects versions
Priority
MinorWho's Looking?
Open Who's Looking?
Details
Details
Assignee
Reporter

There is the patch In attachment that adds test class to show subject error.
The error is
NHibernate.Exceptions.GenericADOException : could not execute query
[ select children1_3_.Pregnant as col_0_0_ from t_animal animal0_ inner join t_animal children1_ on animal0_.Id=children1_.ParentId where children1_.ANIMAL_TYPE='MAMMAL' ]
[SQL: select children1_3_.Pregnant as col_0_0_ from t_animal animal0_ inner join t_animal children1_ on animal0_.Id=children1_.ParentId where children1_.ANIMAL_TYPE='MAMMAL']
----> System.Data.SqlClient.SqlException : The multi-part identifier "children1_3_.Pregnant" could not be bound.
Query to get this error in test:
var list = session.Query<Animal>()
.SelectMany(o => o.Children, (animal, animal1) => animal1)
.Where(o => o is Mammal)
.Select(o => ((Mammal) o).Pregnant)
.ToList();