views:

1031

answers:

1

I have an application that has a core assembly with base classes that I need to inherit from.. I need to save these to the database and after reading about NHibernate decided to use it.

However I have a problem with one of my new inherited classes.. I have setup the subclass map but when I save, it neither attempts to save any of it's base class properties or any of it's new ones that I have assigned in the mapping!

My classes are laid out like the following: (from a small demo app)

core assemblies DataItem -> User

Anything that will touch the database inherits the DataItem class as it handles the id, modified date etc etc..

In my test I setup user to only have a FirstName..

If I save a new User it works great.. however when I inherit from user and then add another property called LastName and attempt to save this new object.. it only puts a sql statement together of INSERT INTO t_User (id) VALUES(?).. it doesn't attempt to save the first name or last name.. either though both have been set and are mapped.

My nhibernate.config:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="DAL">
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="adonet.batch_size">16</property>
    <property name="current_session_context_class">web</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <mapping assembly="DAL"/>
    <mapping assembly="NHibernateDemo"/>
  </session-factory>
</hibernate-configuration>

As you can see I have 2 assemblies.. my DAL is my core and the NHibernateDemo is a web application that uses the core for inheritance.

My core DataItem mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DAL" namespace="DAL.Model">
  <class name="DataItem" table="t_DataItem" >
    <id name="Id">
      <generator class="native" />
    </id>
    <discriminator column="typeid" type="System.Int32"></discriminator>
    <property name="IsActive" column="isActive" not-null="true" />
    <property name="TypeId" column="typeId" not-null="true"></property>
    <many-to-one name="Parent" column="ParentId" class="DataItem"></many-to-one>
    <bag name="Children" cascade="all-delete-orphan">
      <key column="ParentId"></key>
      <one-to-many class="DataItem"/>
    </bag>
    <joined-subclass name="User" table="t_Users">
      <key column="id"></key>
      <property name="FirstName" column="firstName" not-null="true" ></property>
    </joined-subclass>
    <joined-subclass name="Email" table="t_Emails">
      <key column="emailid"></key>
      <property name="Address" column="Address"></property>
    </joined-subclass>
  </class>
</hibernate-mapping>

My inherited NewUser mapping that doesn't work!:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateDemo" namespace="NHibernateDemo.Model">
  <subclass name="NewUser" extends="DAL.Model.User, DAL" discriminator-value="1">
    <property name="LastName" column="LastName"></property>
  </subclass>

Why is it that when I attempt to save my class NewUser that it doesn't attempt to save any of the other properties set, whether from it's base or newly declared properties?

I'd really appreciate any help or insight to this.. I must be missing something really simple and I just can't see it.

Thanks,

Mike

+1  A: 

It could be that you're not able to mix subclass and joined-subclass mappings in the same class hierarchy. Since your DataItem and User are related by joined-subclass, you may need to make your NewUser class another joined-subclass of User.

Another issue might be the use of the "discriminator" in your current NewUser mapping. The discriminator should be an additional column in your User table that NHibernate uses to tell the difference between a User record and a NewUser record. I'm not sure if this works where the User base class might not specify its discriminator value, whereas the NewUser does. I'm not sure if you're specifying the discriminator-column anywhere, which might also be a problem.

I would suggest first trying to make NewUser a joined-subclass of User.

Andy White
Maybe I am misinterpreting the use of the word 'joined'. Does that not indicate that it will attempt to do a join while doing say a select query? my newuser class saves it's data in the same table as the user class.. there is a type id but it belongs to the dataitem.. which i see could be a problem.i'm going to try what you suggested!!! thanks for the direction.
mike
Ok.. I just tried switching my NewUser mapping to be a joined-subclass of user and it still attempts to save into my user table with the following "INSERT INTO t_Users (id) VALUES(?) .. so it's not trying to save the firstname or lastname properties..
mike