views:

205

answers:

1

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.

+1  A: 

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.

Quibblesome