3 Replies Latest reply on May 21, 2003 5:38 PM by Jon Martin Solaas

    cmr - remote vs local

    bhau Newbie

      I am new to the ejb world. I have problems with cmr-fields. I have 2 entity beans that extend ejbRemoteHome. My ejb-jar.xml is pasted below. I get a class cast exception when i try to create a choice(an entity bean). I know it is so because I must use local interfaces to entity beans and i am using remote interfaces. But my question is a) why should it matter? b) how do i get around this c) can i have a remote interface and a local interface update and access the same field in a relational database? How do i go ahead cause i have been stuck on this for a long time.

      Any help, suggestion is highly appreciate.

      Thanks,
      ~Bhau


      <ejb-jar>
      <display-name>Choice</display-name>
      <enterprise-beans>
      <entity>
      <display-name>Options</display-name>
      <ejb-name>Options</ejb-name>

      <home>OptionHome</home>
      <remote>Option</remote>
      <ejb-class>OptionBean</ejb-class>

      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>option</abstract-schema-name>
      <cmp-field><field-name>id</field-name></cmp-field>
      <cmp-field><field-name>context</field-name></cmp-field>
      <cmp-field><field-name>reason</field-name></cmp-field>
      <cmp-field><field-name>Val</field-name></cmp-field>
      <primkey-field>id</primkey-field>

      </entity>

      <entity>
      <display-name>Choice</display-name>
      <ejb-name>Choice</ejb-name>

      <home>ChoiceHome</home>
      <remote>Choice</remote>
      <ejb-class>ChoiceBean</ejb-class>

      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>choice</abstract-schema-name>
      <cmp-field><field-name>id</field-name></cmp-field>
      <cmp-field><field-name>context</field-name></cmp-field>
      <cmp-field><field-name>user</field-name></cmp-field>
      <primkey-field>id</primkey-field>

      </entity>
      </enterprise-beans>

      <relationships>
      <ejb-relation>
      <ejb-relation-name>option-choice</ejb-relation-name>

      <ejb-relationship-role>
      <ejb-relationship-role-name>option-has-choices</ejb-relationship-role-name>

      <multiplicity>One</multiplicity>

      <relationship-role-source>
      <ejb-name>Option</ejb-name>
      </relationship-role-source>

      <cmr-field>
      <cmr-field-name>choices</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>

      <ejb-relationship-role>
      <ejb-relationship-role-name>choice-refers-to-option</ejb-relationship-role-name>

      <multiplicity>Many</multiplicity>
      <cascade-delete/>

      <relationship-role-source>
      <ejb-name>Choice</ejb-name>
      </relationship-role-source>

      <cmr-field>
      <cmr-field-name>option</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>
      </relationships>

        • 1. Re: cmr - remote vs local
          Adrian Brock Master

          The common solution to this is to write
          a session facade.

          You should be able to find resources on this
          pattern by searching the web.

          Regards,
          Adrian

          • 2. Re: cmr - remote vs local
            bhau Newbie

            Thank you for the response? Isn't there any other way to deal with this problem. The thing is I can't use session beans (a requirement).

            Thanks,
            bhau

            • 3. Re: cmr - remote vs local
              Jon Martin Solaas Newbie

              > Thank you for the response? Isn't there any other way
              > to deal with this problem. The thing is I can't use
              > session beans (a requirement).

              Well, first this must be a weird project, as bad design is a requirement :-)

              Anyway, you can implement simple business methods with remote interface in your entity beans "bypassing" this problem. What you can't do is passing local interfaces to remote clients, and cmr only works thru' local interfaces.

              Say you have a recursive relationship in the CMP bean Person; a get/setParent and a get/setChildren. Now, the getChildren method will return a reference to a Collection thru the local interface. A client then will be able to update the actual collection directly using for instance getChildren().add(someChildLocalHome). Because remote interfaces passes return values and parameters by value and not reference this won't work thru' a remote interface.

              Instead, in the remote interface of the Person bean you will create the methods addChild(String childID) and removeChild(String childID). In the implementation of those methods, inside the Person EJB, you will manipulate the CMR collection just like any client using a local interface would. You will look up the PersonLocalHome using findByPrimaryKey and add the found Person to the relation collection using getChildren().add(aPersonLocalHomeFoundByPrimaryKey)

              See http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/CMP5.html

              and chapter 11 in "Mastering EJB" (available at the serverside)

              --
              jonmartin.solaas@mail.link.no
              www.objectlabs.no