I have an entity, say A. And another one B.
A={id,name} B={id,details}
I want a one-to-one relation. Basically for each x from A there must be exactly one y from B and the reverse. So I thought about solutions and came up with the following.
1. The id is shared.
But then basically it will be like.
A={same_id,name} B={same_id,details} same_id is unique
- B will have a foreign key for same_id to A.same_id.
- Or A will have a foreign key for same_id to B.same_id
But I want to be able to get the details from A. Basically doing $a->getAB()->getDetails(); Which means that we need to implement 2).
But that sounds counterintuitive since that will mean, one must first create a new B entity and then create an A entity and point it to the B entity.
Which looks like B owns A - and is not what I want.
And in the reverse - if B has a foreign key to A, can I still tell A about B's existence, so I can do $a->getAB()->getDetails(); ?
2. B has another id, and A has a foreign key to it:
So it will be like: A={id,name,bId} B={bId,details} bId is unique, id is unique
But in this way - it seems like I am wasting a column for A. B won't know it's attached to A too which perhaps is a bad practice.
EDIT: But this doesn't solve any problem, just noticed I must again first create B, since any x from A needs to point to a valid y from B.
3. Is the problem about the automatic generation of my mappings?
Currently I am generating my XML mappings and entity classes from my MySQL database.
Could it be that this generation won't automatically tell A if B has a foreign key to it?