Hi,
I have read the Parent-Child Principles of Hibernate (
http://www.hibernate.org/209.html).
There bi-directional one-to-many associations are discussed mainly.
I have an uni-directional one-to-many association. (There is no reference from the child to the parent in java. And there ist no many-to-one mapping in the mapping hbm of the child) And I have a problem that is described there as well:
Note that uni-directional one-to-many will fail with a forgien key constraint
violation unless the forgein key column in the database is null-able. This is becuase Hibernate first inserts the child row (with a NULL foreign key) and then updates it later.
What is the solution for such an association? To declare the columns as null-able?
This is a bad solution?
You can alternativly declare not just the columns as not-null-able, but also declare the key in the one-to-many mapping als not-null-able.
Hibernate then produces two right insert sqls:
insert the parent
insert the child (with the correct pk of the parent as fk)
and one unnucessary update sql:
update child set pk of parent as fk
The information of the update is allready inserted in the insert statement of the child.
Is it possible to avoid the update statement? Because it does nothing.
Or am I missing something?
Is the update sql needed?
It seems that setting the key as not-null-able makes the update statement obsolete?
Thanks.
jakob