-->
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: one-to-many w/natural key
PostPosted: Mon Nov 15, 2004 12:13 pm 
Newbie

Joined: Thu May 13, 2004 1:37 pm
Posts: 9
Hibernate version: 2.1.6

Can anyone help me out here? I suspect that I'm misunderstanding something pretty basic. My Hibernate in Action (8.3, pp. 330-338) talks about some of this (via composite keys) but I'm having trouble translating those examples into my problem.

I'm trying to establish a One-To-Many relationship between two tables based on a natural key. My situation is this --

The parent class mapping document is:
Code:
<class name="com.example.parent" table="parent">
<id name="id" column="id" type="java.lang.Integer">
    <generator class="identity"/>
</id>
<set name="children" lazy="false">
   <key column="childClaim" foreign-key="parentClaim" />
   <one-to-many class="com.example.child"  />
</set>
<property name="claimNumber" column="parentClaim" type="java.lang.String" />
</class>


The child class mapping is:
Code:
<class name="com.example.child" table="child">
   <id name="id" column="id" type="java.lang.Integer">
      <generator class="identity" />
   </id>
   <property name="claimNumber" column="childClaim" type="java.lang.String" />
</class>

Note that there is not a column in child to map back to the parent's id. The only relationship that I have between the two tables is parent.parentClaim = child.childClaim.

At first I thought about adding a parentId column to the child table and then seeding that column manually. For a variety of reasons that won't work on an ongoing basis. Most importantly the parent record isn't created until after the child records by an old, untouchable COBOL app. So, for the purpose of today's exercise, I have to assume that the schema is static for ever more.

In any case, when I execute a Hibernate query to grab a parent row I'll see Hibernate execute three SQL statements.
The first is a query to get the id of the parent row. No problem. The second is a query to return the entire parent row. Again, no problem. Not optimal, but I'll worry about that later.

It's the third SQL statement that's causing me a problem. It's almost exactly as I'd expect -
Code:
select .... from child children0_ where children0_.childClaim = ?

All well and good, but the ? parameter is being replaced with the primary key of the id of the parent row as opposed to the value of parent.parentClaim as I would expect.

Thanks!


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.