开发者

NHibernate mapping with custom loader doesn't work

开发者 https://www.devze.com 2023-03-03 15:44 出处:网络
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.

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...


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号