This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Simple one-to-one relationship with FK. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Simple one-to-one relationship with FK." Watch "Simple one-to-one relationship with FK." New topic

Simple one-to-one relationship with FK.

Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
I have two tables. Header has a FK to State. Header stores a "state" code. As I fetch Header objects I want a "State" object associated with each Header. I created a "State" property in class Header (with associated getters and setters.) How do I associate these tables? I tried to use the one-to-one element in the Header mapping, but that seems to demand that State have a FK to Header's primary key. How do I associate State with Header's "state_code" property?

<class name="package.Header" table="RT_SAS_HEADER">
<id column="key" name="key" type="int" >
<generator class="assigned"
<property name="state_code" type="long" not-null="true" />

<class name="package.State" table="RT_RETR_PARCELS">
<id name="code" type="long">
<generator class="assigned" />
<property name="abbreviation" type="String" length="2" not-null="true" />
<property name="name" type="String" length="54" not-null="true" />
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Here's what I ended up doing:

"state_code" is the column that has the FK to the other record. In addition, I had to change the original state_code declaration to be:

The "insert" and "update" parameters are needed to let Hibernate know that the join value would never change.
I agree. Here's the link:
subject: Simple one-to-one relationship with FK.
Similar Threads
one-to-many relation
Exception when having different class and table name
problem with hibernate
Hibernate - Mapping foreign keys tables
Hibernate- one-to-many association on a foreign key