-->
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.  [ 1 post ] 
Author Message
 Post subject: How to map identity keys to child tables?
PostPosted: Fri Aug 14, 2009 7:43 pm 
Newbie

Joined: Wed Nov 08, 2006 2:54 am
Posts: 4
I'm trying to map a legacy database using Spring.NET 1.2.0 and NHibernate 2.0.1 and one of the relationships is giving me fits so I'm hoping someone can point me in the right direction. The major issue is that some of the tables have their own unique keys as well as foreign key relationships back to the parent table. The basic schema for these tables is as follows:

CREATE TABLE Parent
(
Id int identity(1, 1) not null,
...
constraint pk_Parent primary key clustered (Id)
)

CREATE TABLE ChildA
(
Id int not null,
...
constraint pk_ChildA primary key clustered (Id),
constraint fk_ChildA_Parent foreign key (Id) references Parent (Id)
)

CREATE TABLE ChildB
(
Id int identity(1, 1) not null,
ParentId int not null,
...
constraint pk_ChildB primary key clustered (Id),
constraint fk_ChildB_Parent foreign key (ParentId) references Parent (Id)
)

CREATE TABLE ChildC
(
Id int identity(1, 1) not null,
ParentId int not null,
...
constraint pk_ChildC primary key clustered (Id),
constraint fk_ChildC_Parent foreign key (ParentId) references Parent (Id)
)

CREATE TABLE ChildBC
(
ChildBId int not null,
ChildCId int not null,
ParentId int not null,
...
constraint fk_ChildBC_Parent foreign key (ParentId) references Parent (Id),
constraint fk_ChildBC_ChildB foreign key (ChildBId) references ChildB (Id),
constraint fk_ChildBC_ChildC foreign key (ChildCId) references ChildC (Id)
)

Note that the relationships here are actually one-to-one meaning there will be one and only one entry in ChildA, ChildB, ChildC, and ChildBC for each Parent so the identity keys in ChildB and ChildC are redundant. I wish I could drop and recreate the schema, but I'm stuck with it.

I don't know if this is "correct", but this is how I have the tables mapped at the moment:

<class name="Parent" table="Parent">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>

<one-to-one name="ChildA" outer-join="true" foreign-key="none" />
<one-to-one name="ChildB" outer-join="true" foreign-key="none" />
<one-to-one name="ChildC" outer-join="true" foreign-key="none" />
</class>

<class name="ChildA" table="ChildA">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
</id>

<one-to-one name="Parent" class="Parent" constrained="true" />
</class>

<class name="ChildB" table="ChildB">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
</id>

<one-to-one name="Parent" class="Parent" constrained="true" />
<property name="ChildBID" type="Int32" not-null="true" generated="insert" insert="false" unique="true">
<column name="ChildBID" sql-type="INT IDENTITY" not-null="true" />
</property>
</class>

<class name="ChildC" table="ChildC">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
</id>

<one-to-one name="Parent" class="Parent" constrained="true" />
<property name="ChildCID" type="Int32" not-null="true" generated="insert" insert="false" unique="true">
<column name="ChildCID" sql-type="INT IDENTITY" not-null="true" />
</property>
</class>

I'm sure this isn't quite the right way to do this and it's probably obvious I don't have a clue what I'm doing, but at least this mapping is inserting the Parent Id into the child tables and fetching the child Ids from the ChildB and ChildC tables.

Is it possible to then have NHibernate magically apply ChildBID and ChildCID into the appropriate columns in the ChildBC table? Also, what is the recommend mapping from Parent to ChildB/ChildC assuming I got everything out of whack?


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

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.