views:

695

answers:

1

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?

Thanks,

---John Putnam

+2  A: 

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

John Putnam