1 Reply Latest reply on Sep 28, 2009 12:18 PM by Juan Isern

    Simple JPA question

    Gianluigi Davassi Newbie


      I'm trying to create a simple INSERT and DELETE test of an entity using JPA:

      
      User user = new User();
      
      userEao = (GenericEAO<User, Long>) cxt.lookup("UserEAO/remote");
      
      User user2 = userEao.persist(user);
      
      userEao.flush();
      
      


      The question is: when user is created should be in TRANSISTENT state, and when i persist it with

      User user2 = userEao.persist(user);


      i suppose that the user2 is a reference of a user in a MANAGED state.

      Actually if i try to delete user with
      userEao.delete(user);


      jboss tell to me:

      [DefaultDeleteEventListener] handling transient entity in delete processing
      


      and it's correct, but if i try to delete the user2 jboss tell to me

      javax.ejb.EJBException: java.lang.IllegalArgumentException: Removing a detached instance
      


      and i dont' understand why the entity user2 is detached!
      so i have BEFORE to merge it ( DETACHED -> MANAGED ) , and than to DELETE, and it works.

      @Override
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public User persist(User entity) {
       getEntityManager().persist(entity);
       return entity;
       }
      


      is it the problem? when the persist finish, it close the transaction and entity became detached?

      i dont want to merge it, cause it causes another select that i want to avoid.
      Or i should i cache the entity instead?

      probably i didn't understand the JPA transactions, what you suggest me to read?

      Thanks, Muzero



        • 1. Re: Simple JPA question
          Juan Isern Newbie

          I think that's because you're looking up the remote interface of userEao.

          Based on my experience, every single invocation to a remote interface (despite the target session bean is in the same environment) creates a new transaction/entitymanager, so the behavior you described above makes sense.

          If you want all your code to span a single transaction just try pulling the local interface from JNDI.