3 Replies Latest reply on Dec 5, 2002 4:38 PM by erik777

    How do I get a class "Many" in a Relationship?

    rafonso

      Hi,

      I have a bean called Country, whose primary key is called countryId and it is a BigDecimal. I have another bean called CountryStates that has a attribute called countryId that is obviously related with Country bean. In my JBuilder 7 I created a Relationship from Country to CountryStates. Thus there is a collection in Country that I called countryStates.
      In ejb-jar.xml, this relationship is showed like above:



      <ejb-relation>
      <ejb-relation-name>country-countryState</ejb-relation-name>
      <ejb-relationship-role>
      country
      <ejb-relationship-role-name>CountryRelationshipRole</ejb-relationship-role-name>
      One
      <relationship-role-source>
      country
      <ejb-name>Country</ejb-name>
      </relationship-role-source>
      <cmr-field>
      countryState
      <cmr-field-name>countryStates</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      countryState
      <ejb-relationship-role-name>CountryStateRelationshipRole</ejb-relationship-role-name>
      Many
      <relationship-role-source>
      countryState
      <ejb-name>CountryState</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      </ejb-relation>



      And in jbosscmp-jdbc.xml, this relationship is showed like above:



      <ejb-relation>
      <ejb-relation-name>country-countryState</ejb-relation-name>
      <foreign-key-mapping />
      <ejb-relationship-role>
      <ejb-relationship-role-name>CountryRelationshipRole</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>countryId</field-name>
      <column-name>country_id</column-name>
      </key-field>
      </key-fields>
      <ejb-designer-id>Country</ejb-designer-id>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>CountryStateRelationshipRole</ejb-relationship-role-name>
      </ejb-relationship-role>
      </ejb-relation>



      Well, the Collection countryStates in Country can't has a remote interface (Why? I don't understand this). So to have access to this collection I created a method with remote interface that returns countryStates. When I runned a Client Test, I get this message error: java.lang.reflect.UndeclaredThrowableException: java.io.NotSerializableException: org.jboss.ejb.plugins.cmp.jdbc.bridge.RelationSet
      .
      So how can I correct this? Better: How Can I have access to countryStates Collection?

      Thanks,

      Rafael U. C. Afonso

        • 1. Re: How do I get a class "Many" in a Relationship?
          erik777

          A findBy can be included in a remote interface, with the traditional restrictions that it can only return a collection of the type of bean it's associated with.

          Thus, you can add a findByCountry to your states bean, returning the states belonging to the country specified in the parameter (BigDecimal).

          For your States bean, you can add the ejb-ql query in your ejb-jar as follows:


          <query-method>
          <method-name>findByCountry</method-name>
          <method-params>
          <method-param>java.lang.BigDecimal</method-param>
          <method-params/>
          </query-method>
          <ejb-ql><![CDATA[
          SELECT DISTINCT OBJECT(s)
          FROM states AS s
          WHERE s.country.id = ?1
          ]]></ejb-ql>

          • 2. Re: How do I get a class "Many" in a Relationship?
            rafonso

            My CountryState bean has a method called findByCountryId, that returns a CountryStates collection with same countryId. So, if I understand, it is not necessary to express my relationship like I did. If I have something like findByCountryId() it will be enough. Am I correct?

            • 3. Re: How do I get a class "Many" in a Relationship?
              erik777

              Yes, that will work.

              You may, however, find the need to get rid of foreign key get/sets. I don't completely understand it, don't know if it's a "bug" in JBoss, poor interpretation of spec, or poor spec, but it appears as though the relationships can get confusing to CMP where it will create duplicate foreign key references in SQL for INSERTS. I remedy it by getting rid of the foreign key get/sets completely, and just using object relations.