Reputation: 123
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?
Upvotes: 1
Views: 4080
Reputation: 196
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.
Upvotes: 2
Reputation: 9429
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.
Upvotes: 3
Reputation: 123
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
Upvotes: 5