2 Replies Latest reply on Jul 15, 2002 3:43 PM by Dain Sundstrom

    JBoss 3.0 and CMP 2.0 Relation Issue

    Jeremy T Newbie

      JBoss 3.0
      JDK1.4.0
      MySQL 3.23.49


      Overview
      I have created a test application for testing JBoss 3.0. I have created a one-to-many relation between two entities, Person and IpLog, respectively. I have the results printed via a test JSP: I retrieve a collection of Persons, retrieve their associated IpLog collection and display the data. Everything works great; the Persons are being retrieved from the database correctly, and the correct number of IpLog entries are being returned. However, when I user a java.util.Iterator to iterate over the IpLog collection (which is a relation defined), I just get the FIRST element in the collection ONLY. The correct number of iterations occur, but each one only displays the first IpLog.

      I have attached the SQL representing the tables, the ejb-jar.xml relation entry, the jbosscmp-jdbc.xml relations entry, and the code used to iterate. Any suggestions?

      SQL:

      #
      # Table structure for table 'ip_log'
      #

      CREATE TABLE `ip_log` (
      `person_id` int(11) NOT NULL default '0',
      `ip_log_id` int(10) NOT NULL auto_increment,
      `ip_log_address` varchar(50) NOT NULL default '',
      `ip_log_last_updt_date` timestamp(14) NOT NULL,
      PRIMARY KEY (`ip_log_id`,`person_id`)
      ) TYPE=MyISAM;

      #
      # Table structure for table 'person'
      #

      CREATE TABLE `person` (
      `person_id` int(11) NOT NULL auto_increment,
      `person_first_name` varchar(50) NOT NULL default '',
      `person_last_name` varchar(50) NOT NULL default '',
      `person_email` varchar(50) NOT NULL default '',
      `person_URL` varchar(100) NOT NULL default '',
      `person_username` varchar(20) NOT NULL default '',
      `person_password` varchar(15) NOT NULL default '',
      `person_last_updt_date` timestamp(14) NOT NULL,
      PRIMARY KEY (`person_id`),
      KEY `person_id` (`person_id`)
      ) TYPE=MyISAM;


      ejb-jar.xml entity relation:

      [pre]
      <ejb-relation>
      <ejb-relation-name>person-ipLog</ejb-relation-name>
      <ejb-relationship-role>
      person
      <ejb-relationship-role-name>PersonRelationshipRole</ejb-relationship-role-name>
      One
      <relationship-role-source>
      person
      <ejb-name>Person</ejb-name>
      </relationship-role-source>
      <cmr-field>
      ipLog
      <cmr-field-name>ipLog</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      ipLog
      <ejb-relationship-role-name>IpLogRelationshipRole</ejb-relationship-role-name>
      Many
      <relationship-role-source>
      ipLog
      <ejb-name>IpLog</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      </ejb-relation>
      [/pre]


      jbosscmp-jdbc.xml entity relation:
      [pre]
      <ejb-relation>
      <ejb-relation-name>person-ipLog</ejb-relation-name>
      <foreign-key-mapping></foreign-key-mapping>
      <ejb-relationship-role>
      <ejb-relationship-role-name>PersonRelationshipRole</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>personId</field-name>
      <column-name>person_id</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>IpLogRelationshipRole</ejb-relationship-role-name>
      </ejb-relationship-role>
      </ejb-relation>
      [/pre]


      Code to iterate

      [pre]
      try{

      //initialize JNDI context
      ctx = new InitialContext();

      //Get the local home which contains the life cycle methods...
      PersonLocalHome home = (PersonLocalHome)ctx.lookup("test.Person");
      Collection persons = home.findAllPersons();
      for(Iterator i=persons.iterator(); i.hasNext();){
      PersonLocal person = (PersonLocal)i.next();
      //get IPLog entries associated with person. See ejb-jar.xml for entity relationships

      String firstName = person.getPersonFirstName();
      String lastName = person.getPersonLastName();
      out.println("Person Id: "+person.getPersonId()+"");
      out.println("IP Log entries for "+firstName+":");

      /**
      * NOTE: The Iterator is retrieved and then iterated over in a while loop.
      * You'll receive an IllegalState exception if you try to initialize an
      * iterator within a for loop!
      */
      Collection logs = person.getIpLog();
      Iterator j = logs.iterator();
      out.println("There are "+logs.size()+" IP Log entries");
      while(j.hasNext()){
      IpLogLocal log = (IpLogLocal)j.next();
      out.println("Log id: "+log.getIpLogId()+"");
      out.flush();
      }

      }catch(Exception e){
      out.println("Caught Exception: "+e);
      e.printStackTrace();
      }
      [/pre]

        • 1. Re: JBoss 3.0 and CMP 2.0 Relation Issue
          Jeremy T Newbie

          I found the issue! However, I'm a confused as to why the collection returned by the relation contained "duplicates".

          Here's the solution:

          In my ejb-jar.xml, for the IpLog entity, I had defined the primary key field to be the personId field. The IpLog table has a person_id and ip_log_id fields, both comprising the composite primary key. Since the ip_log_id table is unique, I just changed the primary to to use ipLogId and voila -- the correct data is returned.

          Question

          Can anyone explain why my collection contained the correct number of IpLogs (n) for a specific Person, based on the personId foreign key, but the collection contained n-number of IpLog entries, each representing the FIRST record? For example, if Person 1 had 10 IpLog entries, I'd receive a collection containing the 10 IpLog entries; however, all contained the information of the first record of the 10 IpLog entries. It would seem to me that the container's query would still select the appropriate data, based on the person_id.

          Any suggestions on why would be greatly appreciated.

          • 2. Re: JBoss 3.0 and CMP 2.0 Relation Issue
            Dain Sundstrom Master

            You have completely lost me. Can you simplfy the problem?

            Are you using a custom primary key class? If the hashCode funtion is coded incorrectly, you can get very weird results.