If I create a parent object with a name, and a set of children, and a child object with a name, this is a unidirectional one-to-many relationship pretty similar to what's shown in the documentation.
Now, theoretically, since the Child is persistable alone, its parent key should be nullable. In practice, I can control this to prevent that from happening, by only allowing the external API to pass a Parent to be saved, with no call to save the Child directly.
So if I were creating a schema that works with this structure, I might set the parent key to not-null, even though a Hibernate schema generation would make it nullable by default. There is no way to inform hibernate of this fact in the mapping since the <key> tag inside <set> can't take a not-null attribute.
If I then save a Parent with a Child, it inserts the Parent, inserts the Child (with no Parent key) and updates the Child to include the parent key. If I've made the parent key not null, this immediately breaks and causes a failure.
Is there any way around this other than making it a bidirectional link? It's not the end of the world if that's what I have to do, and it does increase the safety of saving a child directly (by allowing me to specify that the parent key is not null).
I had been leaning towards unidirectional links when not otherwise necessary, just to get around the consequence of having one of them be 'ignored', but this may prevent me from going that route.
Thoughts?
- Geoffrey
_________________ --
Geoffrey Wiseman
|