I have a Location class which references a State class with a composite id. The mapping compiles fine, but when I use SchemaExport to create the schema it always gets the foreign key wrong (it compares the Location.state_code to the State.country_id, and the Location.state_country_id to the State.code). I can even go in and fix the FK and then everything works fine...
I would like to find a better way to set up the mapping which will avoid this issue. Here are the condensed mappings:
Also interesting to note is that I couldn't find a way to tell Hibernate how to resolve which properties of State the columns in Location refer to. Basically I can switch the order of the State columns in the Location mapping and the system will change which column it puts the data in; Hibernate is just using the order make a choice. Switching this order does not change the mapping in the foreign key.
<class name="State" table="states"> <composite-id> <key-many-to-one name="country" class="Country" column="country_id"/> <key-property name="code"/> </composite-id> </class>
<class name="Location" table="locations"> <id name="id" column="id"> <generator class="guid"/> </id> <many-to-one name="state" class="State" > <column name="state_country_id" /> // how does it know which property of State this column refers to? <column name="state_code"/> </many-to-one> </class>
FK ends up trying to compare locations.state_country_id to state.code, locations.state_code to state.country_id
Thank you
|