【问题标题】:nHibernate named query, result transformation and column name with whitespacenHibernate 命名查询、结果转换和带空格的列名
【发布时间】:2012-08-15 17:57:36
【问题描述】:

我有一个名为 nHibernate 的查询,它返回自定义数据。所以我决定做一个bean类来封装数据。这是一些代码:

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; }
} 

和映射:

<?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>

但是当我调用这个方法时,我得到了一个异常:

NHibernate.Exceptions.GenericADOException:无法执行查询 ----> System.IndexOutOfRangeException : [自定义列]

帮忙,有人吗?

【问题讨论】:

    标签: nhibernate nhibernate-mapping whitespace named-query


    【解决方案1】:

    好的,我自己发现了问题。我想,我需要转义包含空格的列名,就像我对表名所做的那样。但显然我不应该。

    这个语法很好。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多