I have a legacy system and I'm trying to use Hibernate as our data access method for a new project that will coexist with the other legacy applications.
Assume I have a class: (ignoring the contrived example...)
Code:
class Person {
Long id;
Person brother;
// other stuff...
}
Assume the table is:
Code:
create table person (
id number primary key,
brother number
);
In my mappings, I've set up a many-to-one relationship. (It doesn't seem right, but it
does seem to work...) i.e.:
Code:
<class name="Person" table="person">
<id name="id" type="java.lang.Long"/>
<many-to-one name="brother" class="Person">
<column name="brother"/>
</many-to-one>
</class>
(Please let me know if there's a better way to create this mapping...)
Here's my problem: If a "person" has no "brother", the "person.brother" column
should be NULL. However, because of some coding errors in other programs, sometimes the brother field is "0" (zero). (There's no foreign key constraint on the field, so the database allows this. Since our legacy code did these lookups manually, we simply worked around the problem by only doing the lookup if the field > 0.) Understandably, when I try to load this object in Hibernate, it complains that it cannot find a row where person.id = 0.
Assuming that "fixing" the problem in the database is not an option, I'm trying to find a work-around that will allow us to use Hibernate.
(I tried creating an Interceptor that would identify this condition. In the Interceptor, I tested if person.brother was not null, and person.brother.id == 0, then set person.brother = null. This didn't work because somewhere prior to the calling of the Interceptor Hibernate must have stored a reference to "Person#0", and still threw an exception when attempting to resolve the reference.)
Any assistance would be greatly appreciated.