4 Replies Latest reply on Jun 4, 2009 4:51 AM by vink

    Problem with CMP

    vink

      Hello,

      My Scenario is as follows,
      - I'm haivng a main SLSB, which is placing a call to child SLSB
      - I'm getting some entities from child SLSB
      - I'm changing those entities
      - And, assuming that CMP will persist those entities

      Result,
      - Sometimes entities get perisisted
      - And, sometimes it doesn't

      I'm not making use of merge(), as you can see from the dummy code shown below; I'm just changing the properties & leaving it to the container for persistence.

      Please suggest.

      Vinay

      @SLSB
      public class ProductBean
      {
       @EJB
       private ComponentBean mComponentBean;
      
       public void renameMyEntity(String anOldName, String aNewName)
       {
       MyEntity entity = mComponentBean.findMyEntity(anOldName);
      
       // change properties of MyEntity
       entity.setName(aNewName);
       }
      }
      
      @SLSB
      public class ComponentBean
      {
       public MyEntity findMyEntity(String aName)
       {
       return ..;
       }
      }
      




        • 1. Re: Problem with CMP
          peterj

          Don't you mean @Stateless, and not @SLSB?

          I assume that you are using EJB3 and JPA. You need to use the entity manager to save the entity, otherwise the database will not get updated.

          • 2. Re: Problem with CMP
            vink

            I mean @Stateless. (Dummy code for understanding only)

            I think! when you are working on an existing entity & you modify that entity. After your parent transaction is over, we expect your entity manager will be flushed automatically & your entity will be updated in the database.

            In my case entity manager is injected in every ComponentBean.

            • 3. Re: Problem with CMP
              justkeys

              I think it all depends on the transaction attributes.
              You have to make sure that the fetching and modification of the entity happen within the same transaction, by annotating the outer ejb with

              @TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRED)
              



              • 4. Re: Problem with CMP
                vink

                Hello Dieter,

                I think, we don't need to specify this attribute explicitly; it is by default.