Hibernate version: 3.3.1
Hy all
I’m trying to map a one-to-one relationship but I can’t find the right match
I’ve got two objects A and B. B has a foreign key pointing to A.
Here’s a pseudo-code
Code:
class A {
long id;
B b;
}
class B {
long id;
}
But the table structure looks like this
Create table A (
id INT PRIMARY_KEYVAR
)
Create table B (
id INT PRIMARY_KEYVAR,
a_id INT
)
So this would be easy if A had a collection of Bs (using a <bag> and a <one-to-many> tags)
Code:
<bag name=”bs” >
<key column=”a_id” />
<one-to-many class=”B” />
</bag>
But it’s not a collection.
I could use a <one-to-one> with a <property-ref> if B had a accessors to A such as getA() and setA()
Code:
<one-to-one name=”b” class=”B” property-ref=”a” />
But it doesn’t
The only way I’ve found is by using a <many-to-one> inside a <join> tag
Code:
<join table="B" >
<key column="id" />
<many-to-one name="b" column="a_id" />
</join>
This however links my two tables threw a join clause and thus can’t perform lazy loading
Is there a way of doing this ?
In short, I need lazy loading on a one-to-one relationship where the accessors are on the opposite side of the relationship to the foreign key?
I hope all this is clear