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!