File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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.
 
Similar Threads
Exception when having different class and table name
Hibernate - Mapping foreign keys tables
one-to-many relation
problem with hibernate
Hibernate- one-to-many association on a foreign key