Hi,
I'm currently working on a project that involves reverse-engineering Hibernate mappings and model objects from an existing (and ugly) database schema. Up until now we've managed to work with the numerous composite keys but have now hit a hurdle.
The basic concept is like the Employee-Manager recursive relationship you see in Database 101 tutorials.
We have a table:
EMPLOYEE {
database_id
employee_id
employee_name
....
manager_id
}
where the primary key is database_id and employee_id combined. Each employee can have a manager (zero or one) who is also an employee and each manager can have many employees (though we're not concerned with this side of the relationship at the moment).
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<composite-id name="id" class="EmployeePrimaryKey">
<key-property name="databaseId" column="database_id"/>
<key-property name="id" column="employee_id"/>
</composite-id>
<property name="employeeName" column="employee_name"/>
... other properties
<many-to-one name="manager" class="Employee" insert="false" update="false">
<column name="database_id"/>
<column name="manager_id"/>
</many-to-one>
</class>
</hibernate-mapping>
This appears to work fine until an employee with no manager is encountered (i.e. manager_id == null). I think the problem occurs because although manager_id is null, database_id always has a value and hence the composite EmployeePrimaryKey is never null, so Hibernate tries to load the manager even though half the Key is null. Looking at the Hibernate src (Component.nullSafeGet()) it seems as though Hibernate only terminates loading if ALL the properties of the primary key are null. The behaviour we are after would be if any of the primary keys are null (this makes sense for a composite key)
Is there any workaround for this without having to modify Hibernate's code?
TIA
|