Hi all
For a particular complicated query, I have reasons to use native SQL, HQL is no option. The examples here are much simplified.
In this simple model:
Code:
User has Projects
Project has ProjectDetails
I want to execute the following SQL script:
Code:
SELECT * FROM User usr LEFT OUTER JOIN Project pr ON pr.Owner = usr.ID LEFT OUTER JOIN ProjectDetail prd ON prd.Proj = pr.ID
To let NHibernate hydrate the entities automatically, I use
Code:
session.CreateSQLQuery(query)
.AddEntity("usr", typeof(User))
.AddJoin("pr", "usr.Projects")
.AddJoin("prd", "pr.Details");
Unfortunately, the collection Project.Details is not populated by this query. Depending on some details, the collection keeps either uninitialized (it hits the DB when I access it), or it is initialized with an empty collection, although the project
has some ProjectDetails.
After spending nearly two days of trying to get this to work, I just want to ask whether something like that is actually possible to do with NHibernate. If it is, I will post further details.
And, I
do know that fetching in this way is possible with HQL. I have reasons to do it in plain SQL.
Thank you!
Jonas