I'll be short. I'm using Hibernate 3. My problem is about one-to-one mapping with composite-id's (I think). None of the examples I have seen so far are the same as my situation.
Goal
Without any mapping in the database itself, being able to do:
Code:
Apple apple = // retreive apple from Hibernate
String juice = apple.getOrange().getJuice();
Problemorg.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class fruit.Orange, got class fruit.Apple
Why does it not work? He should expect an Orange in the mapping, but why is there an Apple?
DatabaseI have two tables without any foreign key constraints. For APPLE the primary key is number and subnumber. For ORANGE the primary key is nr and subnr.
APPLE (number, subnumber, color)
ORANGE (nr, subnr, juice)
MappingCode:
<class name="fruit.Apple" table="APPLE">
<composite-id>
<key-property name="number" column="number" type="long" />
<key-property name="subnumber" column="subnumber" type="int" />
</composite-id>
<one-to-one name="orange" class="fruit.Orange" />
</class>
Code:
<class name="fruit.Orange" table="ORANGE">
<composite-id>
<key-property name="number" column="nr" type="long" />
<key-property name="subnumber" column="subnr" type="int" />
</composite-id>
</class>
JavaCode:
public class Apple {
private long number;
private int subnumber;
private Orange orange;
// getters and setters
}
Code:
public class Orange {
private long number;
private int subnumber;
// getters and setters
}