Reputation: 3683
I have a class mapped with NHibernate and I'm trying to use a custom sql-query for loading. Special thing is that it uses a composite-id but I would expect that that's not a problem here.
Here's a simplified version of the mapping:
<class name="Person" mutable="false">
<composite-id>
<key-property name="PropertyA" column="propA" type="int" />
<key-property name="PropertyB" column="propB" type="string" />
</composite-id>
<property name="PropertyC" column="propC" type="datetime" />
<loader query-ref="loadPersons" />
</class>
<sql-query name="loadPersons">
<return class="PV" />
<![CDATA[
SELECT propA, propB, propC FROM MyPersons
]]>
</sql-query>
The problem is that the loader is completely ignored. The query sent to the database is completely generated as if the <loader>
element would not be there:
SELECT this_.propA, this_.propB, this_.propC FROM Person this_
This obviously results in the error: table or view does not exist (because 'Person' is just our clean name).
Anyone knows if this is related to the composite-id or is there another reason why loader would be ignored?
Please note that there may be an error in returning the data as well. I've seen there's a special alias-syntax for that but I can't figure that out before the loader is actually doing something...
Upvotes: 2
Views: 832
Reputation: 1580
I know this is a very old question, but I was looking for the answer and came up with something. You use a query, wrapped in parentheses, as the table. It's a little ugly, but it works for me:
<class name="Person" mutable="false" table="(SELECT propA, propB, propC FROM MyPersons)">
<composite-id>
<key-property name="PropertyA" column="propA" type="int" />
<key-property name="PropertyB" column="propB" type="string" />
</composite-id>
<property name="PropertyC" column="propC" type="datetime" />
</class>
Upvotes: 2