1 Reply Latest reply on Nov 19, 2002 4:31 PM by zarni

    The iterator of a CMR collection may only be used within the

    erikture

      Hello!

      I am using Jboss 3.0.4 and CMP 2.0 Entity beans.
      I know that you have to be in the same transaction as you have used to fetch you related CMP beans but I do not understand the following.

      In my session beans I am collecting my beans throw a normal finder method. Then I whant to produce plain java objects that I return to the users. Just to avoid that my code is getting to messy I have divided this into several private methods. One where I iterate my cmr beans. I know that that could be done converting the collection to an ArrayList, but what do I do if I whant to step on step further, i e jump into another cmr that I receive throght the first CMR.

      In some way the transaction seems to go away if I pass my collection/set between private methods in the same session bean.

      Below there is some pseudoe code:


      public SomeObject[] getAllObjects(){
      Collection all = home.findAll();
      Iterator iter = all.iterator();
      SomeObject[] allObjects = new SomeObject[all.size()];
      int index=0;
      while(iter.hasNext()){
      RelatedOjbect rel = traverseRelation(all.getRelatedObjects());
      allObjects[index] = new SomeObject(rel);
      }
      return allObjects;
      }

      private RelatedOjbect traverseRelation(Set relatedOjbects){
      Iterator iter = relatedOjbects.iterator();
      while(iter.hasNext()){
      RelatedObjectLocal rel = (RelatedObjectLocal )iter.next();
      return new RelatedOjbect(traverseAnother(rel.getAnother()));
      }
      }

      private AnotherOjbect traverseAnother(Set anotherOjbects{

      Iterator iter = anotherOjbects.iterator();
      while(iter.hasNext()){
      AnotherObjectLocal rel = (AnotherObjectLocal )iter.next();
      return new AnotherOjbect(rel);
      }
      }


      /Erik

        • 1. Re: The iterator of a CMR collection may only be used within
          zarni

          I have a similar problem.

          I have a collection of beans that has a cmr field. If i try to iterate over the cmr field i get this error, but only in JBoss 3.0.4...

          original exception as thrown by jboss
          >java.lang.IllegalStateException: A CMR collection may only >be used within the transction in which it was created


          If I use JBoss3.0.0 this exception is not thrown.

          here's an example of what I'm doing...

          let's say Item is the bean i'm iterating over and while iterating I call a CMR get method:

          ArrayList relatedBeans = new ArrayList( Item.getCMRbeans );

          this work's fine but when I try to iterate over this new collection and do some get methods on it I get that ugly exception.

          Maybe it's just JBoss 3.0.4 making the problem...

          I don't know...
          Someone please help.

          zarni

          oh... and by the way transction should be transaction!!!!
          :-) never mind, I like such errors, reminds me on Microzoft