Question

I'm developing a web- application using NHibernate. Can you tell me how to write a NHibernate Query for the following SQL query:

SELECT v1.Id
FROM VIEW v1
LEFT JOIN VIEW v2 ON v1.SourceView = v2.Id
ORDER BY v1.Position

It's basically a auto-join but I don't know how to write this in Nhibernate. Lets say the property names are the same as the table column names.

Was it helpful?

Solution

You could just perform the select on the original entity and make the association between the two objects "lazy = false". As long as the entities are mapped then both will be returned and you wont get a lazyloadingexception when trying to access the object.

If you don't want to map "lazy=false" then you can also iterate through the results and perform some sort of operation (such as asking if it is null; if(v1.AssocatedObject == null){}) to ensure the data is loaded while the session is open.

Update:

I think there is actually a better one than that in, NHibernateUtil.Initialise() that can initialise a collection without having to wander through it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top