I understand that this question is 5 years old, and the “correct” answer is definitely that you cannot do this with QueryOver, as the other answers indicate. However, if you really need this feature (like me), there is a suitable workaround that I found.
The solution is to use a “loader query” with native SQL in your XML mapping to create the appropriate collection (see http://nhibernate.info/doc/nhibernate-reference/querysql.html#querysql-load ). In a specific OP example you should go and display your DatabaseView as an object, as suggested, and then write the following in your display:
<class name="MyObject"...> ... <set name="MyViews" inverse="true"> <key column="ObjectId" foreign-key="none"/> <one-to-many class="MyObject"/> <loader query-ref="myObjectViewsLoadQuery"/> </set> </class>
Then we just need to define our called myObjectViewsLoadQuery in raw SQL to explain NH how to join the two:
<sql-query name="myObjectViewsLoadQuery"> <load-collection alias="view" role="MyObject.MyViews"/> SELECT view.* FROM DatabaseView view WHERE view.ObjectId = :id </sql-query>
Now we can pretend that in our request there is a “real” collection called MyViews , relating MyObject to DatabaseView :
MyObject alias = null; DatabaseView view = null; var results = session.QueryOver<MyObject>(() => alias) .JoinAlias( () => alias.MyViews, () => view ) //.Where( () => view.Property == "myValue" ) // optionally, restrict the view etc. .List();
Of course, this is a lot of trouble if you only care about the "elegant" request. However, if the reason you use QueryOver is because you want to be able to accept arbitrary input expressions to filter your DatabaseView or various similar actions, this works very well.
source share