Search code examples
nhibernatenhibernate-mappingwhitespacenamed-query

nHibernate named query, result transformation and column name with whitespace


I've got a named nHibernate query which returns custom data. So I decided to make a bean class to encapsulate data. Here is some code:

public IList<Report> GetReport(int reportId)
{
   return Session.GetNamedQuery("GetReport")
                 .SetParameter("Id", reportId)
                 .List<Report>();
}


public class Report
{
   public virtual string Id { get; set; }
   ...
   public virtual string CustomColumn { get; set; }
} 

and mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly" 
namespace="MyAssembly.Model">
    <class name="Report" table="Report">
        <id name="Id" column="Id">
            <generator class="assigned"/>
        </id>
                ...
        <property column="`Custom column`" name="CustomColumn" />
    </class>

    <sql-query name="GetReport">
       <return class="Report"/>
       <query-param name="Id" type="int" />
       exec GetReport :Id
    </sql-query>
</hibernate-mapping>

But when I invoke this method I got an exception:

NHibernate.Exceptions.GenericADOException : could not execute query ----> System.IndexOutOfRangeException : [Custom column]

Help, anyone?


Solution

  • Ok, I found the problem myself. I thought, that I need to escape column names which contains spaces with ` as I do for table names. But apparently I shouldn't.

    This syntax is fine.

    <property column="Custom column" name="CustomColumn" />