2 Replies Latest reply on May 13, 2002 8:24 PM by larsbj

    Problem getting value of foreign key field

    larsbj

      I'm using CMP2.0/Jboss 3.0 RC2. The application consists of two beans: CompanyEJB and UserEJB.

      CompanyEJB can have users, and UserEJB has a company; a typical one-to-many-relationship.

      The company bean has the following access for getting the users:

      public Collection getUsers();

      The field in UserEJB that links to a company is 'company' and I thus got a method in UserEJB for getting the company:

      public CompanyLocal getCompany();

      Now, I have to create an HTML form to add users to a company, and I need to get the _value_ of the company field in the database. As it is now, I only get the Company-bean from UserEJB and I then have to call getCompanyId() on CompanyEJB. Not very efficient and elegant...

      Is there a way I can define a method in the UserEJB that will allow direct access to the foreign-key field?
      I really don't want to write a DAO just to get this field. :-)

      I'm attaching the relationship-section of the ejb-jar.xml:



      <ejb-relation>
      <ejb-relation-name>Company-User</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>company-has-users</ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>CompanyEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>users</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>user-belongsto-company</ejb-relationship-role-name>
      Many
      <cascade-delete/>
      <relationship-role-source>
      <ejb-name>UserEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>company</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>