views:

238

answers:

2

I have a set of tables, with a .hbm.xml for each.

I tried to put in a named query but it would not compile. I moved the code to a CreateQuery and get.

DB_Portfolio is not mapped [select sum(p.Shares * s.Price) from DB_Portfolio p, DB_Securities s where p.AccountNumber = :accountNumber and p.CUSIP = s.Cusip]

The CreateQuery statement looks like.

  IQuery queryBack = session.CreateQuery("select sum(p.Shares * s.Price) from  DB_Portfolio  p, DB_Securities  s where  p.AccountNumber = :accountNumber and p.CUSIP = s.Cusip");

queryBack.SetString("accountNumber", accountNumber);


  return  queryBack.UniqueResult<Decimal>();

I have in the DB_Portfolio .hbm.xml

<many-to-one name="Security" class="BDM_Controller.Source.ORM.DB_Securities, BDM_Controller" column="Cusip"/>

with a foreign key in Portofoio with security on Cusip.

What am I missing here?

Visual Studio 2008, NHibernate 2.1.0.4000, MS SqlServer 2005

Thanks,

---John Putnam

A: 

I did calculated columns in the hibernate mapping file. See the formula in the example below:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="MyModule.DisciplineDTO, MyModule" table="Disciplines">
    <id name="Id" column="DisciplineID" length="15">
      <generator class="assigned"/>
    </id>
    <property name="Preference" formula="'TM.D.'+DisciplineID" update="false" insert="false"/>
  </class>
</hibernate-mapping>

The update and insert attributes tell nHibernate this column is not to be used when constructing update and insert statements if this object is written to the database. You should specify the data type of the column as well.

Jay
That looks good. But in this example it is a query joining two tables together and getting a value from both. A query should work but what I can't figure out is why it thinks the portfolio table is not mapped.
John Putnam
Can you share your mapping file? Did you check the obvious stuff? Set the properties on the mapping file to embedded? Included the mapping in the correct assembly? Added the assembly to the nhibernate helper class so it knows where to load it from?
Jay
A: 

It looks to me that DB_Portfolio is your database table name, and not the class name. In your HQL query you should use the classname and not the database table name.

If "DB_Portfolio" actually is your classname: is the build action of your .hbm.xml file set to "embedded resource"? (Please do if it isn't).

This is a guess, since you did not post the DB_Portfolio mapping file. Please post the complete mapping file and the class definition if you want a more sophisticated answer.

Jan Willem B
That hits the nail on the head. A step I often overlook, the embedded resouce setting. I seem to have another problem with that though. When I open the web site in Visual Studio 2008 on all the selected files in the properties window I only see the misc category which only gives me full name and full path options. That is true for all aspx and vb pages in the site as well. What could cause that to happen?The table is Accounts, the class is indeed DB_Accounts, I like to keep those names seperate so you know which your dealing with. Thanks for your help.---John Putnam
John Putnam