-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Stuck with joined-subclass
PostPosted: Sun Mar 25, 2007 7:55 am 
Newbie

Joined: Sun Mar 25, 2007 7:45 am
Posts: 1
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?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 5:34 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
the table that contains the sub-class must have an identity column that is a PK-FK with the base class table. Thus, EntityID is the same as WebsiteID.

-devon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.