Mapping native sql to Entity with NHibernate

♀尐吖头ヾ 提交于 2019-12-11 12:22:57

问题


Is possible to map a raw sql to an entity with NHibernate?

Like the following:

C# entity:

public virtual IList<Result> MyResult { get; set; }

Hbm.xml:

<bag name="MyResult">
    <my-custom-sql type="Result">
        SELECT * FROM ResultTable where MyComplexCondition = true
    </my-custom-sql>
</bag>

Result has it own hbm.xml. Is possible to do what I want to achieve?


回答1:


There is a feature, called <subselect>. See more here

How to map an NHibernate entity to a query

That could be applied on <class> as well as on <bag>. This could be the way how to use it on a bag for MyEntity which has collection of Languages 

<bag name="Languages" lazy="true" batch-size="25" 
     mutable="false" >
  <subselect>
    SELECT MyEntity_ID, Language_ID, IsNative FROM Languages
  </subselect>
  <key column="MyEntity_ID" />
  <composite-element class="Language">
    <parent name="MyEntity"/>
    <many-to-one not-null="true" name="Language" class="Language" column="Language_ID" />
    <property not-null="true" name="IsNative" column="IsNative"/>
  </composite-element>
</bag>

This of course, should be used for readonly (immutable) solutions



来源:https://stackoverflow.com/questions/33487498/mapping-native-sql-to-entity-with-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!