There are three object entities: GrandParent, Parent and Child
- GrandParent contains grandParentId
- Parent contains object GrandParent, parentId, some attributes, and a list of Child.
- Child contains childId and some attributes.
There are four tables: GrandParent, Parent, Child, ParentsChildrenList
- GrandParent whose primary key is {grandParentId}
- Parent whose primary key is {grandParentId,parentId}
- Child whose primary key is {grandParentId,childId}
- ParentsChildrenList whose primary key is {grandParentId,parentId,childId}
Parent and Child are both identified by composite keys.
How should this relationship be mapped? Note that object Child doesn't contain a reference to Parent nor GrandParant, however table Child uses grandParentId as part of its primary key.
This tables structure satisfies the use cases. However, the mapping isn't straight-forward. Any design insight is appreciated.
[--- Update 1 ---]
I added object GrandParent to Child, and created composite key ChildId for mapping to the tables.
The question remains: Can Child borrow a piece of its id from Parent? That way I don't need to introduce GrandParent in Child purely for persistence.
[--- Update 2 ---]
I removed object GrandParent from Child, but kept the ChildId and the method that allows grandParentId to be set. Things still work as expected. I suspect this can't be reduced further.