Getting "Named Query Not Know" Error on session.GetNamedQuery()

4.4k views Asked by At

I am consistantly getting a "Named Query Not Known" MappingException when calling session.GetNamedQuery(). I'm using Fluent with NHibernate 3.0 and I have the query in a hbm.xml file. To make things simple, i have everything in the same assembly. I've set the Build Action on the xml file to "Embedded Resource".

My Configuration looks like this:

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");

My GetFirstParty.hbm.xml file looks like this:

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

What am i missing here???

Please help.

Thanks,

Mike

1

There are 1 answers

1
rebelliard On

You need to include HBM mappings in your fluent configuration:

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();