1 Reply Latest reply on Feb 5, 2006 4:41 PM by Frank Langelage

    how to deal with "A CMR collection may only be used within t

    John Woo Newbie

      Hi,

      Two local EJB represent 2 tables,

      Department (Dep_id, name)
      Employee (Emp_id, Dep_id, name)

      Department has 1 to many relationship with Employee. so

      DepartmentBean has a method

      public abstract Collection getEmployee();

      in jbosscmp-jdbc.xml, I defined:
      <ejb-relation>
      <ejb-relation-name>department-employee</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>DepartmentRelationshipRole</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>departmentId</field-name>
      <column-name>DEPARTMENT_ID</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>

      then I'm using a servlet (in same ear as ejb.jar in), but when trying to do the following:

      // lookup no problem
      Collection c = d.getEmployee();

      JBoss shows error:

      java.lang.IllegalStateException: A CMR collection may only be used within the transction in
      which it was created
      at org.jboss.ejb.plugins.cmp.jdbc.bridge.RelationSet.getIdList(RelationSet.java:66)
      at org.jboss.ejb.plugins.cmp.jdbc.bridge.RelationSet.access$000(RelationSet.java:32)
      at org.jboss.ejb.plugins.cmp.jdbc.bridge.RelationSet$1.(RelationSet.java:359)
      at org.jboss.ejb.plugins.cmp.jdbc.bridge.RelationSet.iterator(RelationSet.java:357)
      at MyServlet.init(MyServlet.java:73)

      ...


      ---
      Can any help?

      Thanks lots in advance

      John
      Toronto

        • 1. Re: how to deal with
          Frank Langelage Master

          The message "A CMR collection may only be used within the transction in which it was created!" was often discussed here.

          Creating the collection and iterating through it have to be don in the same transaction.
          From a servlet you'll probably have to use a user transaction.
          UserTransaction transaction = ...lookup( "UserTransaction");
          transaction.begin();
          Collection c = d.getEmployee();
          Iterator iter = c.iterator();
          while ( iter.hasNext() ) {
          // do something useful
          }
          transaction.commit();