0 Replies Latest reply on Apr 14, 2006 11:22 AM by aborland

    Using a Stateless Session Bean to call an Entity Bean...

    aborland

      In short, I'm having a problem persisting objects using a Stateless Session Bean, calling a factory, that calls another POJO, which makes a call on an entity manager. The problem is in my create method in my UserBusiness object. Here is my Code:

      My stateless bean:
      ----------------------------------------------
      package com.medq.platform.enterprise;

      import javax.ejb.Stateless;
      import javax.ejb.Interceptors;

      import com.medq.platform.user.IUser;
      import com.medq.platform.user.UserFactory;

      /**
      * Service Object Bean Implementation for EnterpriseService.
      *
      */
      @Stateless
      @Interceptors (com.medq.platform.service.interceptor.ServiceInterceptor.class)
      public class EnterpriseServiceBean implements EnterpriseServiceRemote {


      public void createUser(IUser user) {
      UserFactory uf = new UserFactory();
      try {
      uf.create(user);
      }catch(Exception e) {
      e.printStackTrace();
      }
      }

      }

      -------------------------------
      My factory object....

      package com.medq.platform.user;

      import com.medq.platform.exception.*;

      /**
      * Value Object Factory for User.
      *
      */
      public class UserFactory {

      private IUserBusiness bizObject;

      /*
      * UserFactory Constructor
      */
      public UserFactory() {
      this.bizObject = new UserBusiness();
      }

      //Generic Methods

      /*
      * Creates a User.
      */
      public IUser create(IUser vo) throws ApplicationException, BusinessException {
      return this.bizObject.create(vo);
      }


      }

      And now the POJO that uses the entity manager...


      package com.medq.platform.user;

      import javax.persistence.*;
      import com.medq.platform.exception.*;
      import java.util.*;

      /**
      * Business Object Implementation for User.
      *
      */
      public class UserBusiness extends AbstractUserBusiness {

      //Constructor
      public UserBusiness() {
      super();
      emf = Persistence.createEntityManagerFactory("pu1", new HashMap());
      em = emf.createEntityManager(PersistenceContextType.EXTENDED);

      }

      private EntityManager em;

      private EntityManagerFactory emf;


      //Generic Methods

      /*
      * Creates a User.
      */
      public IUser create(IUser vo) throws ApplicationException, BusinessException {
      em.getTransaction().begin();
      em.persist(vo);
      em.getTransaction().commit();
      return vo;
      }


      public void destroy()
      {
      em.close();
      emf.close();
      }

      }

      Here's where the problem comes in...when my create() in UserBusiness is this:

      em.getTransaction().begin();
      em.persist(vo);
      em.getTransaction().commit();

      I get org.hibernate.PersistentObjectException: detached entity passed to persist: com.medq.platform.user.User. The object does seem to persist, but I'm getting this exception. Is there any way to make this problem go away?? I have tried a bunch of stuff, but obviously, I'm missing something.

      My goal (down the road) is for the Stateless Session Bean to make a bunch of calls to Factories that make calls on an entity manager (actually calls on another object, i.e. UserBusiness, that makes a call on the entity manager) . I want to rollback all of the transactions if one goes bad, and I have a feeling the way I have it set up, it won't work. Any ideas? My first objective is to get this exception to go away...next is to find out how to rollback all the transactions done from the SSB. Thanks in advance.

      Any help would be greatly appreciated.

      --Andrew