3 Replies Latest reply on Sep 19, 2007 11:19 AM by David Copeland

    Foreign Key fields not set for child entity

    Frank Langelage Master

      My parent Entity looks like this:

      public class MbiMsghd implements java.io.Serializable
      {
       /** Field msghdSerial. */
       @Id
       @GeneratedValue(strategy=GenerationType.IDENTITY)
       @Column(name="msghd_serial")
       private java.lang.Integer msghdSerial;
       /** Relation with MbiMsgpa. */
       @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="msghdSerial")
       @OrderBy("msghdSerial,partNo")
       private java.util.Collection<MbiMsgpa> mbiMsgpa = new java.util.ArrayList<MbiMsgpa>();
      


      My child entity like this:
      @Entity
      @IdClass(value=MbiMsgpa.PK.class)
      @Table(name="mbi_msgpa")
      public class MbiMsgpa implements java.io.Serializable
      {
       /** Field msghdSerial. */
       @Id
       @ManyToOne
       @Column(name="msghd_serial")
       private java.lang.Integer msghdSerial;
      
       /** Field partNo. */
       @Id
       @Column(name="part_no")
       private java.lang.Integer partNo;
      


      So the child has a composite primary key and one part of this PK is a FK to the parent.

      In my session bean I can create the parent successfully, but the created and added child did not get set the PK/FK fields.
      MbiMsgpa mbiMsgpa = new MbiMsgpa();
       mbiMsghd.getMbiMsgpa().add( mbiMsgpa );
       // set all attributes of child entity
       this.entityManager.persist( mbiMsgpa );
      

      The key values of the child entity are null when they are inserted into the database.

      From all examples I saw in my book and on the Web I expected the EJB3 container to set the key fields of the child entity.
      It does not make any difference if I set up the OneToMany relation uni- or bidirectional.

      Did anyone of you get something like this running on JBoss 4.2.2.GA?


        • 1. Re: Foreign Key fields not set for child entity
          wayne baylor Apprentice

          you will have to set the child's key manually.

          • 2. Re: Foreign Key fields not set for child entity
            Frank Langelage Master

            Is this a restriction of the JBoss version or what?
            All examples I saw on the net an in the book I bought didn't do this.

            • 3. Re: Foreign Key fields not set for child entity
              David Copeland Newbie

              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.