4 Replies Latest reply on Apr 5, 2006 10:28 AM by chornsey

    Adding children that do not exist.

    chornsey

      I have a prent object peron.

      This object is mapped as a onetomany with two sets of children.
      The children objects are phonenumber and address.

      They are mapped as a list and manytoone, so the relationship is bidirectional.

      The cascade is all.

      If I run the following code in a SLSB the behavior is very add.

      Person person = new Person ("joe", "smith");
      person.getAddresses().add(new Address("1111", "test"));
      person.getAddresses().add(new Address("2222", "test"));
      person.getAddresses().add(new Address("3333", "test"));
      person.getPhoneNumbers().add(new PhoneNumber("111", "1234567"));

      System.out.println("Addresses Count : " + person.getAddresses().size());
      System.out.println("Phone Number Count : " + person.getPhoneNumbers().size());

      entityManager.persist(person);
      entityManager.flush();
      entityManager.refresh(person);

      System.out.println("Addresses Count After : " + person.getAddresses().size());
      System.out.println("Phone Number Count After : " + person.getPhoneNumbers().size());

      Now when this is run the logs show :

      Addresses Count : 3
      Phone Number Count : 1
      Addresses Count After : 3
      Phone Number Count After : 3

      Whatever the number of records in the address list it creates the same number in the phonenumber list by duplicating the first phonenumber object over and over until it matches the size of the address list.

      If I make one phonenumber and two addresses, jboss persists two address, but copies the phone number twice.

      I am sure this is a bug as the counts are right before I call persist, but change after jboss returns control.