I am having the exactly same problem, though I found a really lame workaround (see below).
Basically, without exposing the foreign key and managing it myself, I have no way to model
OneToMany relationships. This has to be a bug. The O'Reilly EJB3 book explicitly
states that the I should not have to manage the keys.
It seems to be a problem at the Hibernate layer, though I couldn't find anything about
it on the Hibernate forums.
The Java Persistence layer seems to correctly interpret the annotations and foreign keys,
but when Hibernate executes the query, it either omits the foreign key (for unidirectional
relationships) or sets it to null (for bi-directional relationships).
Really Lame Workaround
I'm using MySQL and had my foriegn key field set to not null. This is correct for a
composite relationships (you don't want instances of the child sitting around without
a parent).
By changing my table to allow nulls in the foreign key field, it correctly updates the
databse.
It seems that hibernate (or Java Persistence) wants to insert the rows without foreign
keys first, and then does updates to the table to provide the foreign keys. That is
pretty lame, and seems like a bug, but with all these crazy layers of stuff, I have no
idea to whom to report it.
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4086189#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...