views:

180

answers:

2

I want to have a 'virtual' (not there, not to be confused with the virtual keyword) field in a nhibernate class.

I have a securities table and I want to have a 'virtual' field that uses another field to return a flag.

I have added a calculated field in the db_securities table nhibernate class:

 public class DB_Securities
    {
        private string cusip;
        private string securityDescription;

          ...

        private string classCode;

          ...

        private String isSweepAccount;

          ...


        public virtual string ClassCode
        {
            get { return classCode; }
            set { classCode = value; }
        }


          ...

        public virtual String IsSweepAccount

        {
            get
            {
                isSweepAccount = (classCode > 20 && ClassCode < 25)?"Y":"N" ;
                return isSweepAccount;
             }
            set { isSweepAccount = value; }
        }
    }

I have the DB_Securities.hbm.xml with

  <class name="<namespace>.DB_Securities, <assembly>" lazy="true" table="Securities">

    <id name="Cusip" type="String" length="9">
      <generator class="assigned" />
    </id>

    <property name="SecurityDescription" type="String" length="36"/>

         ...

   <property name="ClassCode" type="String" length="3"/>

        ...

    <property name="IsSweepAccount" type="String" update="false" insert="false"/>

I think this should allow me to access field IsSweepAccount as though it was in the table but I get:

 Inner Exception = Invalid column name 'IsSweepAccount'.

and the SQL showing that it is creating a select and referencing that column name, which should be alright.

I did this very thing in java/hibernate about a year ago succesfully. This is my first try at the same thing in nhibernate.

What am I missing?

A: 

I found the answer. I just needed to drop the IsSweepAccount from the .hbm.xml mapping file so it did not try to map it to a field in the table. From the C# side it appears to be a field in the table.

John Putnam
+1  A: 

I can't see any reason to include that field in the mapping file since it's not being persisted to the database. If you do want to persist it, then you can map the private member isSweepAccount using an access strategy and naming convention.

Jamie Ide

related questions