About three days of research has resulted in my concluding that I have no hope of doing this correctly the first time. I'm asking for advice on how to handle this OR mapping: A table, we'll call MAP, is used to relate the PKs for any two tables. Each row is assigned a discriminator. Therefore, three columns;
Code:
MAP table:
------------
TYPE varchar (PK)
FROM varchar (PK)
TO varchar
Similar patterns appear often when integrating systems; it's too painful or simply impossible to alter some production system to create proper foreign keys, yet one must record that a row was created on behalf of another row, possibly on a different platform. So, you define a TYPE and store the primary key of one table in FROM and the primary key of another table in TO. Multiple TYPEs may exist (possibly for the same pair of tables...) In our case TYPE and FROM form a composite key and the MAP table may provide many-to-one relations (otherwise this could be a many-to-many junction table.)
Given a pair of tables that are related via MAP...
Code:
TABLE_A table:
--------------
ID number (PK)
TABLE_B table:
--------------
ID number (PK)
...we'll define a MAP discriminator 'FOO' that identifies MAP rows like so:
Code:
x = some primary key value from TABLE_A
y = some primary key value from TABLE_B
INSERT INTO MAP (type, from, to) VALUES ('FOO', x, y)
Given that MAP rows which relate TABLE_A and TABLE_B will always have TYPE = 'FOO', how does one model the association in Hibernate?
Should I subclass a MAP base class by the discriminator? Will having the discriminator in the key "work" with Hibernate (2.1)? May I then associate TABLE_A and TABLE_B to the subclasses via; <many|one-to-one><column...></>?
I've tried various forms of composite-id keys on MAP and so far I've been unable to figure out how to associate this to TABLE_A or TABLE_B. The notion of a constant (for the TYPE) as a property appears to be persona non grata in Hibernate.
How would you approach this and can you point to any examples? I feel I've put in the requisite time trying to figure this out, and I'd like to make use of Hibernate, but I need to get past this quandary without too much more time spent experimenting.
Thanks.