开发者

Using nhibernate <loader> element with HQL queries

开发者 https://www.devze.com 2022-12-24 00:18 出处:网络
I\'m attempting to use an HQL query in a <loader> element to load an entity based on other entities.

I'm attempting to use an HQL query in a <loader> element to load an entity based on other entities.

My class is as follows

public class ParentOnly
{
    public ParentOnly(){}
    public virtual int Id { get; set; }
    public virtual string ParentObjectName { get; set; }
}

and the mapping looks like this

<class name="ParentOnly">
  <id name="Id">
    <generator class="identity" />
  </id>
  <property name="ParentObjectName" />
  <loader query-ref="parentonly"/>
</class>

<query name="parentonly" >
  select new ParentOnly() 
  from SimpleParentObject as spo
  where spo.Id = :id
</query>
开发者_JAVA技巧

The class that I am attemping to map on top of is SimpleParentObject, which has its own mapping and can be loaded and saved without problems.

When I call session.Get<ParentOnly>(id) the sql runs correctly against the SimpleParentObject table, and a ParentOnly object is instantiated (as I can step through the constructer), but only a null comes back, rather than the instantiated ParentOnly object.

I can do this succesfully using a instead of the HQL, but am trying to build this in a database independent fashion.

Any thoughts on how to get the <loader> and <query> elements to return a populated ParentOnly object...?

Thanks

Matt


From my understanding of the nHibernate documentation, changing the select statement should fix this problem.

<query name="parentonly" >
  select ParentObjectNameAS {spo.Name}, Id AS {spo.Id}
  from SimpleParentObject as spo
  where spo.Id = :id
</query>

source: http://www.nhforge.org/doc/nh/en/

0

精彩评论

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