简体   繁体   English

nhibernate命名查询,找不到名称

[英]nhibernate named queries , name not found

I'm coming from a Java side of using Hibernate and I just haven't found the proper place to put the named query in NHibernate . 我来自使用Hibernate的Java方面,只是找不到在NHibernate放置命名查询的适当位置。

Using Visual Studio 2008 , C# 2008 使用Visual Studio 2008和C#2008

I have a query 我有一个查询

<query name="SchwabAccountList">
  from DB_Accounts a
  where a.AdminOffCode = 'SWB'
</query>

and I want to put it in the .hbm.xml for the Account table ( DB_Accounts ) 我想将其放在Account表( DB_Accounts )的.hbm.xml

I put it at the end of the file but within the <class> tag 我把它放在文件的末尾但在<class>标记内

    <query name="AccountList">
      from DB_Accounts a
      where a.AdminOffCode = 'SWB'
    </query>
  </class>
</hibernate-mapping> 

The code I am using, I have tried several different ways but get 我正在使用的代码,我尝试了几种不同的方法,但是得到了

Named query not known: AccountList

or whatever other name I tried to use ( assembly.dir.dir.class.queryname ) that sort of thing. 或其他我尝试使用的其他名称( assembly.dir.dir.class.queryname )之类的东西。

The access code looks like. 访问代码如下所示。

              ISessionFactory factory = cfg.BuildSessionFactory();
              ISession session = factory.OpenSession();

              IList<DB_Accounts> accountList = 
                  (IList<DB_Accounts>)(session.GetNamedQuery("AccountList").List());

              foreach (BDM_Controller.Source.ORM.DB_Accounts acctRec in accountList)
              {
                         ...

What am I missing? 我想念什么?

I moved the query outside the tag and got it to recognize the query. 我将查询移到了标签之外,并使其能够识别查询。 I had it at the top of the mapping file earlier and it complained about the following tag. 之前,我将其放在映射文件的顶部,并且它抱怨以下标记。 It might have been a problem with the single quote. 单引号可能存在​​问题。 I added the CDATA wrapper to protect against those issues. 我添加了CDATA包装器以防止这些问题。 So a combination of the two change probably solved the problem. 因此,将两种更改结合起来可能可以解决问题。

I now have: 我现在有:

  </class>
  <query name="AccountList" cacheable="true" read-only="true">
    <![CDATA[
      from DB_Accounts a
      where a.AdminOffCode = 'SWB'
      ]]>
  </query>
</hibernate-mapping> 

and this works 这有效

另外,请确保将映射文件的“构建操作”设置为“嵌入资源”,否则即使映射文件本身正确,也会出现相同的错误。

Also make sure that the hbm file is added with extension .hbm.xml rather than just .xml. 另外,请确保hbm文件添加的扩展名为.hbm.xml,而不仅仅是.xml。 This also will result in the same error. 这也将导致相同的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM