4 Replies Latest reply on Sep 16, 2004 3:58 PM by davout

    How are you supposed to add a child entity in a CMP CMR 1-to

    davout

      I'm a little confused about how a CMP entity bean that has 1-M relationship link to a subordinate entity should handle the creation new subordinate entity instances.

      For instance,...

      class Entity1Bean {
      ejbCreate(....)
      Collection getEntity2List() // returns collection of 'Entity2'
      }

      class Entity2Bean {
      ejbCreate(....)
      }

      At the moment I'm handling the creation of new Entity2 instances by....

      Entity2Home.create(....)

      However, when I then use 'Entity1Home.getEntity2List' to retrieve a list of
      linked sub entities the newly created Entity2 instance is not part of the
      list.

      I then tried adding.....

      aNewEntity1 = Entity2Home.create(....)
      Entity1Home.getEntity2List().add(aNewEntity1);

      ... and it still didn't work. What am I doing wrong?



        • 1. Re: How are you supposed to add a child entity in a CMP CMR

          the Entitiy's home is creating instances of the Entitiy's Remote Interface


          Collection entities = Ent1Home.findBySomething();
          
          Iterator t = entities.iterator()
          
          while(t.hasNext){
          Ent1Remote ent1 = (Ent1Remote)t.next;
          
          ent1.doSomething()
          
          }




          • 2. Re: How are you supposed to add a child entity in a CMP CMR
            davout

            I'm not sure if I understand your point. The CMR based Entity1 collection of Entity2 instances doesn't appear to be updated after an Entity2 instance is separately created.

            • 3. Re: How are you supposed to add a child entity in a CMP CMR

              ok,
              lets say we have a team and player entities.

              use a session bean that preforms a lookup for team's and player's home.

              you can add to the session bean:
              after you create a new player

              public void addPlayer(String player_name ,String team){

              Player player = PlayerHome.create(player_name);

              Team team = home.findByName(team);

              team.add(player);


              }

              while in the Team entity the add() will be implemented as:

              public add(Player player){
              Collection players = getPlayers();
              players.add(player);
              }

              • 4. Re: How are you supposed to add a child entity in a CMP CMR
                davout

                Many thanks for the response...

                I'd sought of guessed the answer might be something like you suggested.

                However, this raises another problem. What if 'Entity2' has a property called 'Color' and the color entity owns a collection of Entity2 objects, like...

                class Entity1Bean {
                ejbCreate(....)
                Collection getEntity2List() // returns collection of 'Entity2'
                }

                class Entity2Bean {
                ejbCreate(....)
                Color getColor // points at related color object
                }

                class ColorBean {
                Collection getEntity2List() // returns collection of 'Entity2' objects
                // that share the same color
                }

                In this case where 'Entity2' has two different entities in which it appears as a collection entry, where would you call the 'add'. Or do you have to call both?