1 Reply Latest reply on Jan 23, 2009 12:12 AM by Rene Felgenträger

    EntityManager - How to get it on a class that is not a SeamComponent

    Marco Santos Newbie

      Hello!


      I'm use to get my EntityManager as normal way on a Hibernate project



      EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();

      EntityManager em = emf.getEntityManager();

      .....

      em.close();

      if im on a Seam component, specified by the @Name(name) annotation, we just have to do:



      @PersistenceContext private EntityManager em;

      But if i want to create a class that manage all the transactions with the database like creation, updates, removes, query's? How can i get the EntityManager? It is suppose to do on other way?


      Thanks a lot!

        • 1. Re: EntityManager - How to get it on a class that is not a SeamComponent
          Rene Felgenträger Newbie

          So far, i can imagine of three ways:


          1) EJB


          @PersistenceContext
          private EntityManager entityManager;
          



          No Seam needed for this injection, as the ejb-container will do it for you


          2) SMPC


          @In
          private EntityManager entityManager;
          


          Seam is needed, as you are injecting the seam managed persistence context.


          3) SMPC


          EntityManager em = (EntityManager)Component.getInstance("entityManager");
          



          Same as before. But in comparison you do not rely on a seam interceptor for the injection.
          Very useful for components having the @ByPassInterceptor


          Hope this helps you a bit...


          Greetings