Hello.
Is there any way to map a one-to-one relashionship via just a foreign key, not a shared primary key?
DB schema is like this:
Code:
TABLE whole(
id_whole INTEGER NOT NULL,
...other attributes go here....,
PRIMARY KEY(id_whole)
);
TABLE part1(
id_part1 INTEGER NOT NULL,
id_whole INTEGER NULL,
...other attributes go here....,
PRIMARY KEY(id_part1),
FOREIGN KEY(is_whole) REFERENCES whole(id_whole)
);
and the id_whole in part1 table is unique via index.
and object model is like this:
Code:
class Whole{
Integer idWhole;
....... other attributes .....
Part1 part1;
}
class Part1{
Integer idPart1;
....... other attributes .....
}
Yes, I know, the shared primary key is the right (or at least preferred) way to do this, and I know the foreign key does not determine multiplicity and so on.
But I have this DB schema and have no control over it.
So is there any way to achive automatic association here?
So far the only way I see to do this is to map this as many-to-one via Set or whatever container I can use, and implement public get/setPart1 methods in the Whole class so they use this container instead of a plain Part1 variable.
But this is definitely not the elegant solution, which complicates the Whole class, and this is obviously a tool-dictated solution, which I tend to consider a bad thing.
Are there any practical ways?