I am a newcomer with nhibernate and a little bit stuck with joined-subclasses. As I see, a joined subclass is a class inherited but with his own table just for the parts which do not belong to its baseclass.
The problem I am experiencing is that nhibernate tries to select properties for an inherited class, but belong to table of the base class.
I have two classes, Entity and Website. Website is derived from Entity, and has an additional property named "Template". For Entity I have a table, with EntityID and TypeID. For Website I have a table "Website" with EntityID and Template.
The Hibernate mappings are:
For Entity:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Modules.Business.Entity, Modules.Business" table="Entity">
<id name="EntityId" column="EntityId" type="System.Int32">
<generator class="native" />
</id>
<property name="TypeId" column="Type" type="System.Int32" />
</class>
</hibernate-mapping>
For Website:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<joined-subclass
name="Modules.Business.Website.Website, Modules.Business.Website"
extends="Modules.Business.Entity, Modules.Business"
table="Website">
<key column="EntityId"/>
<property name="Template" type="String" />
</joined-subclass>
</hibernate-mapping>
When I retrieve the Website object with:
public T Get(int id)
{
Configuration cfg = new Configuration();
cfg.AddAssembly("Modules.Business");
cfg.AddAssembly("Modules.Business.Website");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
return session.Get<T>(id);
}
I notice the following happens when looking at SQL Profiler:
exec sp_executesql N'SELECT website0_.EntityId as EntityId4_0_, website0_.Template as Template5_0_, website0_1_.Type as Type4_0_ FROM Website website0_ inner join Entity
website0_1_ on website0_.EntityId=website0_1_.EntityId WHERE website0_.EntityId=@p0',N'@p0 int',@p0=1
Why is nhibernate looking for the typeID in the Website table? Shouldn't it look for it in the Entity table?
|