I have a data object (Data) and two kinds of key objects (KeyA and KeyB). I need to work with a record which is a pair key-data, i.e. ({KeyA|KeyB}, Data). Every key has to be mapped to exactly one data object and vice-versa.
The question is how can it be achieved via SQL (with correct SQL constraints) and JPA.
It is obvious that some basic foreign-key magic does not work here. For example, if key objects defines foreign key to Data, it breaks the unique mapping condition: it allows sharing of one Data between two keys (if KeyA references Data and KeyB references Data). On the other hand, if Data defines two foreign keys to Keys, there is allowed to reference two different Keys (A and B). Even adding another "adapter" table (KeyA-KeyB-Data) doesn't help too much.