1 Reply Latest reply on Oct 17, 2008 1:04 AM by Mark Thias

    onetomany children do not have join column populated.

    Chris Hornsey Newbie

      Runnning AS 4.0.4 EJB RC5

      I have two classes one a parent the other a child. The child is mapped into the parent using a unidirectional onetomany.

      When I create a new parent, and add a new instance of a child to the parent then persist, a foreign key violation is thrown since JBOSS does not update the children with the generated key of the parent before attempting to insert them.

      Is this the expected behavior (it certainly was not in my case).

      Example code:


      package datatypes;

      @Entity
      @Table(name = "PERSON")
      public class Person{
      private long id = 0;
      private String firstName = null;
      private String lastName = null;
      private List addresses = new ArrayList();

      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name = "IID")
      public Long getId()
      {
      return id;
      }
      public void setId(long id)
      {
      this.id = id;
      }

      /**
      * @return Returns the firstName.
      */
      @Column(name="SZFIRSTNAME")
      public String getFirstName() {
      return firstName;
      }
      /**
      * @param firstName The firstName to set.
      */
      public void setFirstName(String firstName) {
      this.firstName = setString(firstName);
      }

      /**
      * @return Returns the lastName.
      */
      @Column(name="SZLASTNAME")
      public String getLastName() {
      return lastName;
      }
      /**
      * @param lastName The lastName to set.
      */
      public void setLastName(String lastName) {
      this.lastName = setString(lastName);
      }

      /**
      * @return Returns the addresses.
      */
      @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
      @JoinColumn(name="IPERSONID")
      public List getAddresses() {
      return addresses;
      }
      /**
      * @param addresses The addresses to set.
      */
      public void setAddresses(List addresses) {
      this.addresses = addresses;
      }

      }


      Child Class

      package datatypes;

      @Entity
      @Table(name = "ADDRESS")
      public class Address {

      private long id = 0;
      private long parentId = 0;
      private Integer streetNum;
      private String streetName;


      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name = "IID")
      public Long getId()
      {
      return id;
      }
      public void setId(long id)
      {
      this.id = id;
      }

      @Column(name = "IPERSONID")
      public Long getParentId()
      {
      return parentId;
      }
      public void setParentId(long parentId)
      {
      this.parentId = parentId;
      }

      @Column(name = "ISTREETNUM")
      public Integer getStreetNum()
      {
      return streetNum;
      }

      public void setStreetNum(Integer streetNum)
      {
      this.streetNum = streetNum;
      }

      @Column(name = "SZSTREETNAME")
      public String getStreetName()
      {
      return streetName;
      }
      public void setStreetName(String streetName)
      {
      this.streetName = setString(streetName);
      }
      }


      Test Case :
      Person temp = new AdditionalParty();
      temp.setId(0);
      temp.setLastName("LAST NAME");
      temp.setLastName("FIRST NAME");

      PersonAddress a = new PersonAddress();
      a.setStreetNum(1234);
      a.setStreetName("STREET NAME");
      temp.getAddresses().add(a);
      entityManager.persist(temp);
      entityManager.flush();

      Now when I run this an foreign key violation is thrown when hibernate tries to insert the address record. I assumed hibernate would update the children with the parents id based on the onetomany mapping.

      If I remove the code to add the address this code executes as expected and the variable temp has the generated id after calling persist.

      What am I missing here?



      I am surprised I have not seen more questions regarding this behavior.
      Thanks for any help on this issue