Btw, I've found this workaround for mapping my legacy one-to-one and many-to-one associations on join tables in case of hierarchies of joined subclasses:
- Build the Java classes and mapping files as if the associations are MANY-TO-MANY (see section 7.5.3 of Hibernate 3.1.x manual)
Code:
// Address.java
public Set getPersonSet() {
return personSet;
}
public void setPersonSet(Set personSet) {
this.personSet = personSet;
}
...
<!-- Address.hbm.xml -->
<set name="personSet" table="MYJOINTABLE">
<key ..../>
<many-to-many .../>
</set>
- Adds to the Java classes, but NOT to the mapping files, a pair of suitable setter and getter, implemented using the above many-to-many setter and getter, enforcing the "one" max cardinality.
Code:
// Address.java
public Person getPerson() {
Set set = getPersonSet();
if (!set.isEmpty()) {
return (Person) set.iterator().next();
} else {
return null;
}
}
public void setPerson(Set person) {
Set set = getPersonSet();
set.clear();
if (person != null) {
set.add(person);
}
}
In this way the class exposes one-to-one methods, Hibernate does the job of updating the underlying join table, and the getter/setter methods enforces programmatically the cardinality. And all works with hierarchies of classes and with one-to-one or many-to-one associations put at any level of the hierarchy.