7

I have the following named SQL query defined:

<sql-query name="ItemSearch">
    <return class="ItemSearchResult">
        <return-property name="Item" column="ItemId" />
        <return-property name="Distance" column="Distance" />
    </return>
    SELECT
        Items.*,
        dbo.DistanceBetween(Latitude, Longitude, :lat, :long) AS Distance
    FROM Items
    WHERE Contains(Name, :keywords)
    ORDER BY Distance ASC
</sql-query>

Whenever I try to run my application, I get the generic error "Errors in named queries: {ItemSearch}". Is there something obviously wrong here?

The ItemSearchResult class is a very simple wrapper class that looks like this:

public class ItemSearchResult
{
    public Item Item {get; set;}
    public double Distance {get; set;}
}
Kevin Pang
  • 41,172
  • 38
  • 121
  • 173
  • 1
    Ensure that you have a mapping file for ItemSearchResult (i.e) you have defined ItemSearchResult.hbm.xml. If this is not present nHibernate will not know how to map your Select columns to the Properties. – Vishnoo Rath Aug 08 '13 at 15:19

2 Answers2

3

Do you have a correct .hbm.xml for ItemSearchResult? If you use ItemSearchResult in your query, then you need to have a .hbm.xml for it. Just like entities.

Afshar Mohebi
  • 10,479
  • 17
  • 82
  • 126
  • 1
    Just encountered this problem -- had the file there, but it was ItemSearchResult.xml, NOT ItemSearchResult.hbm.xml. Took a long time to see that. – jmoreno Aug 21 '14 at 19:44
  • It's long time from this answer. But you can try add `.hbm` so the extension would be `.hbm.xml`. It may help solve the problem. – Afshar Mohebi Aug 24 '14 at 14:08
1

Here is a sample from my code: The only few things that are different between the NHibernate version and my Hibernate is the auto-import and I would assume the package.

<hibernate-mapping auto-import="true" package="PackageName">
  <class name="Name of class to maptop">
    <composite-id>
      <key-property name="<name of parameter>" type="TYPE"/>
    </composite-id>
    <property name="COLUMNNAME" type="TYPE"/>
  </class>
  <sql-query name="queryName">
        <return alias="dr" class="Name of class to map to"/>
select columnName as {dr.nameofColumn}, 
   from table
 </sql-query>
</hibernate-mapping>

I think the problem that exists in your code is that you did not specifically map out the columns and how they map to your class.

Note: If there are any fields that do not correspond to the NHibernate XML format let me know through the comments. I don't have access to my NHibernate mapping files right now.

monksy
  • 14,156
  • 17
  • 75
  • 124