使用nHibernate命名查询

我在使用命名查询与nHibernate一起工作时遇到了很多麻烦.我的最新问题是收到错误消息“无法执行查询”,没有其他信息.是否有任何完整的示例我可以从某个地方下载,因为所有的教程和文档示例都提供了代码snippits,但只讲述了有关它的工作原理的一半.

这是给我问题的代码.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model.Entities
{
    public class TableInfo
    {
        public string TABLENAME { get; set; }
        public string COLUMNNAME { get; set; }
        #region Overrides
        public override int GetHashCode()
        {
            int result = TABLENAME.GetHashCode();
            result += COLUMNNAME.GetHashCode();
            return result;
        }
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            TableInfo dict = (TableInfo)obj;
            return
                dict.TABLENAME.IsEqual(this.TABLENAME) &&
                dict.COLUMNNAME.IsEqual(this.COLUMNNAME);
        }
        #endregion
    }
}

映射文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model.Entities" assembly="Model" default-lazy="false">

  <class name="Model.Entities.TableInfo, Model" table="UIM_TableColumnInfo">
    <composite-id>
      <key-property name="TABLENAME" column="TABLENAME" type="string"></key-property>
      <key-property name="COLUMNNAME" column="COLUMNNAME" type="string"></key-property>
    </composite-id>
  </class>

  <sql-query name="GetTableInfo">
    <return alias="tableInfo" class="Model.Entities.TableInfo, Model">
      <return-property name="TABLENAME" column="TABLENAME"/>
      <return-property name="COLUMNNAME" column="COLUMNNAME"/>
    </return>
    <![CDATA[
select 
        info.tci_table_name TABLENAME
        , info.tci_column_name COLUMNNAME
        from ALL_TAB_COLS c
        ,( select 'DATE' TYPE_NAME, 'D' data_type_ind from dual
           union select 'NUMBER','N' from dual
           union select 'VARCHAR2','S' from dual
         ) ct
        , UIM_TableColumnInfo info
        where c.DATA_TYPE         = ct.TYPE_NAME (+)
                and   c.column_id is not null
        and UPPER(c.TABLE_NAME)   = :TableName
        and UPPER(c.COLUMN_NAME)  = UPPER(info.tci_column_name (+))
        order by c.column_id
    ]]>
  </sql-query>  

</hibernate-mapping>

致电代码

public List<TableInfo> GetTableInfo(string tableName)
{
    return m_TableInfoRepository
        .NamedQuery("GetTableInfo")
        .SetString("TableName", tableName)
        .List<TableInfo>() as List<TableInfo>;
}

最佳答案 我假设你已经在客户端数据库中的SQL之前进行了测试,所以我想也许我们应该看看里面发生了什么,所以我可以推荐你这个链接;

> Named Query Error
> Using NHibernate and Log4Net in ASP.NET 2.0 applications
> How do I view the SQL that is generated by nHibernate?

希望能帮助到你.

点赞