开发者

Named Query and Inheritance

I'm having some issues when running a named query on NHibernate. The class I'm setting as the return value is Organization - not abstract -, and I have a second class (which inherits from this one) called FullOrgani开发者_Go百科zation. I have a table per concrete class schema and everything else is working just fine, but I keep getting an exception when running the named query (which BTW doesn't provide any details).

The mapping is close to the following:

<hibernate-mapping 
xmlns="urn:nhibernate-mapping-2.2" 
namespace="XXX" 
assembly="XXX" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:nhibernate-mapping-2.2 ..\nhibernate-mapping.xsd" auto-import="false">
<class name="Organization" table="Organizations" lazy="false">
    <id name="Identity" column="Id">
        <generator class="identity"/>
    </id>

    <property name="Name" column="Name" />
    <many-to-one name="OrganizationType" column="OrganizationTypeId"/>
...
            <joined-subclass name="FullOrganization" table="FullOrganizations" lazy="false">
        <key column="OrganizationId"/>
                    ...
            </joined-subclass>
</class>

And the named query goes something like:

<sql-query name="OrganizationSearch" read-only="true" cacheable="false">
<return class="Organization" />
<![CDATA[
  SELECT *
  FROM Organizations o
  INNER JOIN OrganizationTypes ot ON o.OrganizationTypeId = ot.Id 
  LEFT JOIN FullOrganizations fo ON o.Id = fo.OrganizationId
  WHERE 
    // Several Filters Here
]]>

How should I map the query results? Please take into account that returned objects could be instances of Organization or FullOrganization.


If I am understanding the question correctly, do you have two methods for the search? i.e.

SearchOrganisations and SearchFullOrganisations

If answer is yes then I would remove the

<return class="Organization" />

and in code you can use the AliasToBeanResultTransformer

so one method

return Session
    .GetNamedQuery("OrganizationSearch")
    .SetString("Param1", param1)
    .SetResultTransformer(new AliasToBeanResultTransformer(typeof(Organization )))
    .List<Organization >();

and also

return Session
    .GetNamedQuery("OrganizationSearch")
    .SetString("Param1", param1)
    .SetResultTransformer(new AliasToBeanResultTransformer(typeof(FullOrganization )))
    .List<FullOrganization >();

If you have one method then you can set a switch to apply the desired SetResultTransformer

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜