nhibernate命名查询,名称未找到
我来自使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将查询移到标签之外并让它识别该查询。我之前将它放在映射文件的顶部,它抱怨以下标签。可能是单引号的问题。我添加了 CDATA 包装器来防止这些问题。所以这两个改变的结合可能解决了这个问题。
我现在有:
这有效
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:
and this works
另外,请确保您的映射文件“构建操作”设置为“嵌入式资源”,否则即使映射文件本身正确,您也会收到相同的错误。
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.
还要确保 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.