[jboss-user] [EJB 3.0] - Re: Foreign Key fields not set for child entity

davetron5000 do-not-reply at jboss.com
Wed Sep 19 11:19:37 EDT 2007


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#4086189

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4086189



More information about the jboss-user mailing list