-->
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.  [ 4 posts ] 
Author Message
 Post subject: Table-per-subclass inheritance and many-to-one associations
PostPosted: Wed Dec 19, 2007 7:50 am 
Newbie

Joined: Thu Jul 19, 2007 6:49 am
Posts: 13
Hibernate version:

1.2.1.4000

Mapping documents:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Cbr.Dyktion.BusinessEntities" assembly="Cbr.Dyktion.BusinessEntities">
<class name="Candidate" table="DTN_PERSONEN" mutable="false">
<id name="Id">
<column name="ID" not-null="true"/>
<generator class="native">
<param name="sequence">DTN_SEQ_PK</param>
</generator>
</id>
<property name="LegalName" column="GESLACHTSNAAM" />
<many-to-one name="PlaceOfBirth" class="County" column="GEMEENTE_ID_GEBOREN_IN" not-null="true"/>
<many-to-one name="CountryOfBirth" class="Country" column="LAND_ID" not-null="true"/>
<property name="Version" column="VERSIENUMMER" />

<joined-subclass name="Instructor" table="DTN_INSTRUCTEURS">
<key column="PERSOON_ID"/>
</joined-subclass>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

session.Load<Instructor>(id);

Name and version of the database you are using:

Oracle 10g

Description:
We have a Candidate base-class and an Instructor class that derives from it. The database guys have defined a data model in which the Instructor is stored in a separate table with a foreign key pointing back at the Candidate. As far as this is concerned, it looks like NHibernate's table-per-subclass strategy.

However, whenever they think a many-to-one association to an Instructor is needed, they store the ID of the Instructor in the corresponding table. If you then pass this ID into Load<Instructor>(id), NHibernate will not find the record. In fact, you have to pass in the ID of the corresponding Candidate (the base class) to find it.

The problem is that the DB guys don't want to change this. They say that when you need a many-to-one association to an Instructor, then you need to store the ID of the Instructor, not that of a Candidate. Are they correct and is NHibernate wrong, or is it just a matter of preference?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 2:23 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
You might both be wrong. With table-per-subclass, the ID of the subclass table is the ID of the base class table, like a one-to-one relation. They have modelled this as a one-to-many relationship, which is not correct.

This solves the one-to-many relation problem as well.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 2:59 pm 
Newbie

Joined: Thu Jul 19, 2007 6:49 am
Posts: 13
So you mean that the <key> element within the <joined-subclass> is actually referring to its own primary key column? I thought it was the foreign key that refers back to the base class. So basically this one-to-one association is using a primary key association?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 5:31 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
dionysos1973 wrote:
So you mean that the <key> element within the <joined-subclass> is actually referring to its own primary key column?

Yes.
Quote:
I thought it was the foreign key that refers back to the base class.

It is also this.
Quote:
So basically this one-to-one association is using a primary key association?

Yes.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.