0 Replies Latest reply on Jul 19, 2005 4:52 AM by per_nyfelt

    Upgrading from CMP 1.1 to 2.1 - relationships

    per_nyfelt

      I am upgrading a CMP 1.1 based system to CMP 2.1 and are struggling with relationships. I have two beans Activity and ActivityLog where the table structure looks as follows:

      CREATE TABLE Activity (
       id bigint(20) NOT NULL,
       description varchar(75) default NULL,
       entityCode char(2) default NULL,
       active char(1) default NULL,
       seq int(11) default NULL,
       PRIMARY KEY (id)
      )
      


      CREATE TABLE ActivityLog (
       id bigint(20) NOT NULL,
       entityId bigint(20) default NULL,
       activityId bigint(20) default NULL,
       userId bigint(20) NOT NULL default '0',
       activityDate datetime default NULL,
       description varchar(100) default NULL,
       PRIMARY KEY (id)
      )
      


      In the old way I had a finder called findActivitiesForEntity which i defined as follows in jaws.xml:
       <finder>
       <name>findActivitiesForEntity</name>
       <query>, Activity
       WHERE ActivityLog.activityId = Activity.id
       AND ActivityLog.entityId = {0}
       AND Activity.entityCode = {1}
       </query>
       <order>activityDate DESC, id DESC</order>
       </finder>
      


      I found no way of doing it this way on JBOSS 4.0.1 using CMP 2.1 which is my target so I modified the finder to look like this instead (using xdoclet):

      @ejb.finder
       signature="java.util.Collection findActivitiesForEntity(long entityId, java.lang.String entityCode)"
       query="SELECT OBJECT(o) FROM ActivityLog AS o WHERE o.entityId = ?1 AND o.activity.entityCode = ?2 ORDER BY o.activityDate DESC, o.id DESC"
      


      Thus using the idea of a relationship between ActivityLog and Activity to be able to navigate from one to the other. However i cannot figure out how to declare the CMR relationship. The following code does NOT work as JBoss thinks it is a M:N relationship (which it is not).
       /**
       * @ejb.interface-method
       *
       * @ejb.relation
       * name="Acitivity-ActivityLog"
       * role-name="ActivityLog-belongs_to-Activity"
       * target-role-name="ActivityLog-Activity"
       * target-ejb="Activity"
       *
       * @jboss.target-relation
       * related-pk-field="id"
       * fk-column="activityId"
       *
       */
       public abstract Activity getActivity( );
      
       /**
       * @ejb.interface-method
       */
       public abstract void setActivity( Activity activity );
      


      I've not defined any CMR field in Activity (actually tried that as well without any luck). Essentially i want to navigate from ActivityLog to Activity but a re not really interested in going the other way (from Activity to activityLog).

      Any help would be most appreciated!

      Best regards,
      Per