I have a situation where I have multiple possible parent objects, all of which could potentially contain the same sort of child objects.
Example:
public class person {
private string id;
private string name;
private Set Cars;
}
public class dealership {
private string id;
private string name;
private Set cars;
}
public class corporation {
private string id;
private string name;
private Set cars;
}
public class car {
private string id;
private int owner_class; // one of person, dealership, corporation
}
What's the best way to map this using hibernate? Right now I'm using:
<class name="Owner" table="owner" mutable="false">
<cache usage="read-only" />
<id name="id" type="string" length="32">
<column name="id" length="32" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" type="string"/>
<set name="cars" table="cars">
<key>
<column name="owner_id" length="32"></column>
</key>
<many-to-many class="Car">
<column name="car_id" length="32"/>
</many-to-many>
</set>
</class>
<class name="Corporation" table="corporation" mutable="false">
<cache usage="read-only" />
<id name="id" type="string" length="32">
<column name="id" length="32" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" type="string"/>
<set name="cars" table="cars">
<key>
<column name="owner_id" length="32"></column>
</key>
<many-to-many class="Car">
<column name="car_id" length="32"/>
</many-to-many>
</set>
</class>
This doesn't seem to work though because hibernate tries to put multiple foreign key constraints on the owner_id column of the join table.
Can anyone suggest a viable solution?
|