Linq/Hql/Criteria Fetch on class hierarchy generates invalid SQL when derivees mix component and many-to-one mappings
Description
Environment
Attachments
is related to
Activity

Andrei Alecu May 18, 2011 at 2:22 AM
I reviewed the fix, but it seems to be only for NH-2615. This particular issue () isn't fixed. You seem to have removed the relevant tests that were pertaining to it, so that unit test folder should instead be NH2615.
So, even if this isn't supported (although everything else seems to work except for when Fetch is used), I think this one shouldn't be marked fixed, because that query still generates invalid sql. Maybe "won't fix"/"by design", or maybe let it stay open if someone wants to still fix it some time in the future.
However, in my case, I was mainly interested in NH-2615, so - thanks Fabio!

Fabio Maulo May 17, 2011 at 10:43 AM
Ok, but take care with polymorphic queries (asking for the base type shared by component and entity).

Andrei Alecu May 16, 2011 at 8:58 AM
Thanks Fabio.
I understand the considerations, but I think I took the necessary precautions (only having Id in the many-to-one subclass, separate subclasses, etc), and it seems to work in our application otherwise, except for this fetch issue.
I'm mostly just interested in NHibernate being able to ignore the Fetch on component, so I can have repository methods that can take either subclass and not throw an invalid join exception.
Less concerned about the invalid SQL right now.

Fabio Maulo May 16, 2011 at 8:15 AM
Andrei,
for this test there is no problem but, as advise, don't try to put a component with an entity in the same hierarchy; the case is not supported by neither ObjectRelationalMapping nor NHibernate and is not supposed to work always.

Fabio Maulo May 16, 2011 at 7:53 AM
That is right.
I'll comment on that issue.
Consider the following scenario:
public class ItemBase
{
public virtual int Id { get; set; }
public virtual SubItemBase SubItem { get; set; }
}
public class ItemWithComponentSubItem:ItemBase { }
public class ItemWithManyToOneSubItem:ItemBase { }
ItemWithComponentSubItem defines in its mapping that SubItem is a component of SubItemComponent type (which derives from SubItemBase)
ItemWithManyToOneSubItem defines in its mapping that SubItem is a many-to-one of SubItemEntity type (again, derives from SubItemBase)
Running the following query with fetch results in invalid SQL being executed:
from ItemBase i left join fetch i.SubItem
Removing the fetch makes it work properly.
from ItemBase i
Also, this works:
from ItemWithManyToOneSubItem i left join fetch i.SubItem
But this doesn't:
from ItemWithComponentSubItem i left join fetch i.SubItem (one would argue there's nothing to fetch here, but instead of throwing an exception, the fetch instruction should just be ignored and maybe a warning logged, it would allow for much better reuse of code)
I will attach a unit test that demonstrates this bug, and also a related one at NH-2615.
I believe the correct behavior here is to allow fetch on components, and simply ignore it rather than throwing an exception.