4 Replies Latest reply on Sep 8, 2011 4:33 PM by lightguard

    Seam Persistence Entity Manager Injection

    alexland

      I'm developing a web application based upon JEE6 and Seam 3.
      The web application consists of single war.


      I implemented an entity manager producer as said in Seam persistence reference guide, like below:




      @ConversationScoped
      public class EntityManagerProducer implements Serializable {
      
           private static final long serialVersionUID = -1425976074150887643L;
      
           public static final String UNIT_NAME = "pu";
      
           @PersistenceUnit(unitName = UNIT_NAME)
           private EntityManagerFactory entityManagerFactory;     
      
              @ExtensionManaged
           @Produces
           @ConversationScoped
           public EntityManager produce() {
      
                EntityManager entityManager = entityManagerFactory.createEntityManager();
      
                      // .. do somthing with entity manager
      
                return entityManager;
      
           }
           public void dispose(@Disposes EntityManager entityManager) {
                if (entityManager.isOpen()) {
                     entityManager.close();
                }
           }
      
      }



      Moreover I have a Repository/DAO base class, with EntityManager injection




      public abstract class AbstractRepository<T> implements Repository<T> {
      
      
           @Inject
           private EntityManager em;
      
           @Override
           public EntityManager getEm() {
                return em;
           }
           ...
      }



      Real repository implementation extends this class.


      Now the problem is:


      I have an EJB annotated with @Singleton @Startup that uses a real repository implementation to load some object in a @PostConstract annotated method.
      Here no conversation context is active, and as said in Weld guide to EJB are propagated only RequestScope and Application Scope.


      Well, is there a way to produce an entity manager by basing on the invocation context, in other words
      can I produce a ConversationScoped entity manager when processing jsf request and TransactionScoped manager with no jsf request?


      Thanks in advance for the answer


      Alex