aspose file tools*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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
Author

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?

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

State
<hibernate-mapping>
<class name="package.State" table="RT_RETR_PARCELS">
<id name="code" type="long">
<generator class="assigned" />
</id>
<property name="abbreviation" type="String" length="2" not-null="true" />
<property name="name" type="String" length="54" not-null="true" />
</class>
</hibernate-mapping>
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.
 
wood burning stoves
 
subject: Simple one-to-one relationship with FK.