nhibernate命名查询,名称未找到

发布于 2024-08-08 03:06:15 字数 1215 浏览 4 评论 0原文

我来自使用 Hibernate 的 Java 端,只是还没有找到在 NHibernate 中放置命名查询的合适位置。

使用 Visual Studio 2008、C# 2008

我有一个查询

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

,我想将其放入帐户表 (DB_Accounts) 的 .hbm.xml 中,

我将其放在最后文件的但在 标记内

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

我正在使用的代码,我尝试了几种不同的方法,但得到

Named query notknown: AccountList

或我使用的任何其他名称尝试使用(assembly.dir.dir.class.queryname )之类的东西。

访问代码看起来像。

              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)
              {
                         ...

我缺少什么?

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.

Using 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)

I put it at the end of the file but within the <class> tag

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生不灭 2024-08-15 03:06:15

我将查询移到标签之外并让它识别该查询。我之前将它放在映射文件的顶部,它抱怨以下标签。可能是单引号的问题。我添加了 CDATA 包装器来防止这些问题。所以这两个改变的结合可能解决了这个问题。

我现在有:

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

这有效

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

飞烟轻若梦 2024-08-15 03:06:15

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

Also, make sure that your mapping file Build Action is set as "Embedded Resource", or you'll get the same error even if the mapping file itself is correct.

初与友歌 2024-08-15 03:06:15

还要确保 hbm 文件添加扩展名 .hbm.xml 而不仅仅是 .xml。这也会导致同样的错误。

Also make sure that the hbm file is added with extension .hbm.xml rather than just .xml. This also will result in the same error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文