5 Replies Latest reply on May 5, 2005 9:38 PM by Robert Paris

    Why does this CMR code not work?

    Robert Paris Newbie

      OK, I have a BookBean that contains a collection of ChapterBeans. Now when
      you call BookHome.create(...), in ejbPostCreate, it does the following:


      public void ejbPostCreate(String title, Collection chapters)
      {
       Collection newChapters = new ArrayList();
       try
       {
       ChapterHomeLocal homeLocal = ( ChapterHomeLocal ) new
      InitialContext().lookup( "ChapterHomeLocal" );
      
       //THESE ARE REMOTE INTERFACES
       Iterator it = chapters.iterator();
       while ( it.hasNext() )
       {
       ChapterRemote c = ( ChapterRemote ) it.next();
       ChapterLocal cL = homeLocal.findByPrimaryKey( ( Integer )
      c.getPrimaryKey() );
      
       newChapters.add( cL );
       }
       //NOW ADDING TO CURR. COLLECTION
       Collection curr = getChapters();
      
       //THE ERROR OCCURS HERE
       curr.addAll( newChapters );
       }
       catch (....elided)
      }
      


      When I do that, it throws a TransactionRolledbackException (at the
      curr.AddAll()) with the message:

      "Unexpected token in statement [SELECT TITLE, CHAPTER_NUMBER, BOOK_ID FROM
      CHAPTER WHERE (ID=?)]"

      HOWEVER, if I do the below code instead, it works!


      public void ejbPostCreate(String title, Collection chapters)
      {
       Collection newChapters = new ArrayList();
       try
       {
       ChapterHomeLocal homeLocal = ( ChapterHomeLocal ) new
      InitialContext().lookup( "ChapterHomeLocal" );
       //CREATE NEW CHAPTER
       ChapterLocal newC = homeLocal.create( "NEW CHAP", 1 );
       newChapters.add( newC );
      
       //NOW ADDING TO CURR. COLLECTION
       Collection curr = getChapters();
       curr.addAll( newChapters );
       }
       catch (....elided)
      }
      
      


      So this tells me it's not a relationship-code/descriptor problem. Is it a
      transaction issue? However, when I change the methods' transaction
      attributes to Never (just to check), it then throws an
      IllegalStateException (as expected), so I don't think it's a transaction-
      scope thing. What am I not understanding here?

      Thank you for ANY help you can give!