• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Simple one-to-one relationship with FK.

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
There is no "i" in denial. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic