Using nhibernate <loader> element with HQL queries

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-03-25T21:01:31Z Indexed on 2010/03/25 21:03 UTC
Read the original article Hit count: 431

Filed under:
|
|
|

Hi All

I'm attempting to use an HQL query in 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>

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(id) the sql runs correctly against the SimpleParentObject table, and the 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 and elements to return a populated ParentOnly object...?

Thanks

Matt

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping