Problem with One-To-Many CMR

Discussions

EJB programming & troubleshooting: Problem with One-To-Many CMR

  1. Problem with One-To-Many CMR (3 messages)

    Hi,

    I have a OneToMany relationship between Person and Pet.
    I can assign a pet to a person, if the pet does not exists in DB (using addNewPet method. But when I use addPet to assign an existing pet (that already is assigned to a person) to another person, the pet is removed from its relationship but the new relationship is not created.

    Here are the methods to assign the pets:

      public void addPet(String name) {
    try {
    InitialContext initial = new InitialContext();
    PetLocalHome plh = (PetLocalHome)initial.lookup("java:comp/env/ejb/local/Pet");
    PetLocal pl = plh.findByPrimaryKey(name);
    this.getPets().add(pl);
    }
    catch (Exception e) { e.printStackTrace(); }
      }
      public void addNewPet(String name) {
    try {
    InitialContext initial = new InitialContext();
    PetLocalHome plh = (PetLocalHome)initial.lookup("java:comp/env/ejb/local/Pet");
    PetLocal pl = plh.create(name);
    this.getPets().add(pl);
    }
    catch (Exception e) {e.printStackTrace();}
      }


    Here's the client I use:

    InitialContext ctx = new InitialContext();
    Object objRef = ctx.lookup("Person");
    PersonHome home = (PersonHome)PortableRemoteObject.narrow(objRef, PersonHome.class);
    Person p;
    p = home.create("Violetta");
    p.addNewPet("Jenny");
    p.addNewPet("Salva");
    p = home.create("Frank");
    p.addPet("Jenny");

    And here's my DD part for the relationships.


      <relationships>
        <ejb-relation>
    <ejb-relation-name>Person-Pet</ejb-relation-name>
    <ejb-relationship-role>
            <ejb-relationship-role-name>PersonHasPet</ejb-relationship-role-name>
            <multiplicity>One</multiplicity>
            <relationship-role-source>
              <ejb-name>PersonEJB</ejb-name>
            </relationship-role-source>
            <cmr-field>
              <cmr-field-name>pets</cmr-field-name>
              <cmr-field-type>java.util.Collection</cmr-field-type>
            </cmr-field>
          </ejb-relationship-role>
    <ejb-relationship-role>
            <ejb-relationship-role-name>PetBelongsToPerson</ejb-relationship-role-name>
            <multiplicity>Many</multiplicity>
            <cascade-delete />
            <relationship-role-source>
              <ejb-name>PetEJB</ejb-name>
            </relationship-role-source>
          </ejb-relationship-role>
    </ejb-relation>
      </relationships>


    Hope that anyone can help
    TIA
    Frank
  2. Problem with One-To-Many CMR[ Go to top ]

    Ok, I found out something strange. If I do the addPet operation twice in the client the new relationship is created, too.

    p = home.create("Violetta");
    p.addNewPet("Jenny");
    p = home.create("Frank");
    p.addPet("Jenny");
    p.addPet("Jenny");

    May this be a timing problem? Or is there a command I could execute to ensure all data manipulation is populated to the DB. (Like close() in JDBC)

    TIA
    Frank
  3. Problem with One-To-Many CMR[ Go to top ]

    Hi,

    I tested again with N-M relationship defined in DD. Then it works ok. Only for One2Many relations
    I get the strange behaviour described in OP.

    And ideas?

    TIA
    Frank
  4. Problem with One-To-Many CMR[ Go to top ]

    hi!

    If u wanna create new relationship between existing person and pet, there are two solutions.
    If u wanna keep old relationship u should use N-M.
    If u can cancel old rel. and wanna bind new rel. change add operation to change operation.