I am having problems getting NHibernate to generate a SQL query that actually runs without errors, as the query is missing joins for my subclasses.
Lets take this minimal example:
class Page
{
public virtual int Id { get; set; }
public virtual string UrlSegment { get; set;}
public virtual Page Parent { get; set; }
}
class ContentPage
{
public string Content { get; set; }
}
Now, imagine a straightforward field-for-field, joined-subclass mapping of this to the database. I came up with this query to find all pages along the path /page/page2/page3:
SELECT p1.* FROM page p1
WHERE
p1.parent IS NULL
AND p1.hierarchysegment = 'test'
UNION
SELECT p2.* FROM page p1
JOIN page p2 ON p2.parent = p1.id
WHERE
p1.parent IS NULL
AND p1.hierarchysegment = 'test'
AND p2.hierarchysegment = 'test2'
UNION
SELECT p3.* FROM page p1
JOIN page p2 ON p2.parent = p1.id
JOIN page p3 ON p3.parent = p2.id
WHERE
p1.parent IS NULL
AND p1.hierarchysegment = 'test'
AND p2.hierarchysegment = 'test2'
AND p3.hierarchysegment = 'test3'
;
Now, this seems to work ok when running the query. Lets try generating this with a SqlQuery (simplified, only shows the first part):
IQuery q = session.CreteSQLQuery("SELECT {p0.*} FROM page {p0} WHERE {p0.Parent} IS NULL" +
"AND {p0.HierarchySegment} = 'page1'", "p0", typeof(Page));
This generates the following query:
SELECT p0.ID AS id0_0_,
p0.enabled AS enabled0_0_,
p0.linktext AS linktext0_0_,
p0.hierarchysegment AS hierarch4_0_0_,
p0.tooltiptext AS tooltipt5_0_0_,
p0.PARENT AS parent0_0_,
p0_1_.content AS content1_0_,
CASE
WHEN p0_1_.ID IS NOT NULL
THEN 1
WHEN p0.ID IS NOT NULL
THEN 0
END AS clazz_0_
FROM page p0
WHERE parent0_0_ IS NULL
AND hierarch4_0_0_ = page1;
But this query contains SQL errors because NHibernate tries to fetch fields which are only available after NHibernate has also added joins for the subclass, ContentPage. Is it possible to somehow add these joins without relying on a specific naming style that NHibernate happens to be using (ie, generating the necessary joins manually)?