Hi,
I have two classes in a one-to-one relationship but I don't want them to have to have the same PK. So I want to have one have a FK to the other's PK like this:
Code:
CREATE TABLE guide (
id integer primary key
);
CREATE TABLE survey (
id integer primary key,
guide_id integer not null unique,
foreign key(guide_id) references guide
);
In java, I want each class to have a reference to the other class.
In Survey.hbm.xml I have:
Code:
<many-to-one
name="guide"
class="org.etp.Guide"
column="guide_id"
not-null="true"
unique="true"
cascade="none"
outer-join="auto"
/>
So far so good. I think I'm using the new feature of 2.1 with the following in Guide.hbm.xml:
Code:
<one-to-one
name="survey"
class="org.etp.Survey"
foreign-key="guide_id"
outer-join="auto"
cascade="delete"
/>
However, Guide.survey is not populated by Hibernate and looking at the SQL generated it looks like the join is still on survey.id=guide.id:
Code:
select _vote0_.id as id__, _vote0_.owner_id as owner_id__, votersur1_.id as id0_, votersur1_.guide_id as guide_id0_, votersur1_.key as key0_, votersur1_.live as live0_, _vote0_.id as id1_, _vote0_.general_date as general_2_1_, _vote0_.primary_date as primary_3_1_, _vote0_.runoff_date as runoff_d4_1_, _vote0_.owner_id as owner_id1_, _vote0_.partner as partner1_, _vote0_.title as title1_, _vote0_.html as html1_ from guide _vote0_ left outer join survey votersur1_ on _vote0_.id=votersur1_.id where _vote0_.owner_id=?
So my first question is whether this is indeed what 2.1 means with the foreign-key attribute of <one-to-one>. And if so why isn't it working?
- Jesse