nhibernate - 在 session.GetNamedQuery() 上获取 "Named Query Not Know"错误

标签 nhibernate session hbm

我在调用 session.GetNamedQuery() 时始终收到“命名查询未知”MappingException。我在 NHibernate 3.0 中使用 Fluent,并且在 hbm.xml 文件中有查询。为了简单起见,我将所有内容都放在同一个程序集中。我已将 xml 文件上的构建操作设置为“嵌入式资源”。

我的配置如下所示:

var nhConfig = Fluently.Configure()
                    .Database(SQLAnywhereConfiguration
                  .SQLAnywhere10
                  .ConnectionString("uid='dba'; pwd='sql'; dsn=db"))
                  .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "thread_static"))
                  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Party>())
                  .BuildConfiguration();

            var sessionFactory = nhConfig.BuildSessionFactory();


            ISession session = sessionFactory.OpenSession();
            CurrentSessionContext.Bind(session);


            NHibernate.IQuery q = session.GetNamedQuery("GetFirstParty");

我的 GetFirstParty.hbm.xml 文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

  <query name="GetFirstParty">
    <![CDATA[from Party p where p.CaseNumber = :CaseNumber]]>
  </query>

</hibernate-mapping>

我在这里错过了什么???

请帮忙。

谢谢,

迈克

最佳答案

您需要在您的流畅配置中包含 HBM 映射:

var nhConfig = Fluently.Configure()
                  .Database(SQLAnywhereConfiguration
                  .SQLAnywhere10
                  .ConnectionString("uid='dba'; pwd='sql'; dsn=db"))
                  .ExposeConfiguration(c => c.SetProperty(Environment.CurrentSessionContextClass, "thread_static"))
                  .Mappings(m => 
                  {
                    m.FluentMappings.AddFromAssemblyOf<Party>();
                    m.HbmMappings.AddFromAssemblyOf<Party>();
                  })
                  .BuildConfiguration();

关于nhibernate - 在 session.GetNamedQuery() 上获取 "Named Query Not Know"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5116617/

相关文章:

nhibernate - SaveOrUpdate 与 NHibernate 中的更新和保存

nhibernate - 使用 NHibernate 正确映射多态关系

django - 使用 Django session 存储登录用户

java - Hibernate - 从注释到 hbm.xml 的级联类型

hibernate - 设置属性的 HQL 查询

NHibernate.PropertyValueException : not-null property references a null or transient

c# - 使用 Fluent nhibernate 自动映射继承

session - 我怎样才能获得所有开放的 session ?

oracle - ORA-12519,TNS :no appropriate service handler found

java - 是否可以在 HBM 中的 SET 元素内定义默​​认对象?