9 Replies Latest reply on Aug 29, 2006 1:10 PM by Gavin King

    EntityManager newibe Question

    dudududu Novice

      I've a Pojo class , non a seam component
      how can I retrieve the EM from the context?

      I tried to use this line:

      EntityManagerFactory emf = Persistence.createEntityManagerFactory("globalEM");
      return emf.createEntityManager();

      and it works , but it start validate my DB everytime I call it
      any other solution?
      Thanks

        • 1. Re: EntityManager newibe Question
          Maurice Zeijen Master

          Use the @PersistenceContext annotation.

          Example:

          
          class SomePojo {
          
           @PersistenceContext
           private transient EntityManager entityManager;
          
           .....
          
          }
          
          


          I advice you to read the EJB3 documentation or take a look at the nice http://trailblazer.demo.jboss.com/EJB3Trail/ website.

          Knowlage about EJB3 is really needed if you want to use Seam.

          • 2. Re: EntityManager newibe Question
            Eric Ray Novice

            Sounds like you should just have the EJB container inject it like so.

            @PersistenceContext(unitName="your_persistence_unit")
            private EntityManager em;
            


            The new EJB3 spec allows for container injection. I think they call it Inside Out Container (IOC) or something like that. Anyway, it's build on annotation from the 1.5 JDK. You can do quite a lot of this type of inject without seam. Seam builds on this concept and also introduces Outjection.



            • 3. Re: EntityManager newibe Question
              dudududu Novice

              I tried that , I am getting null :(
              it's not a session bean, just a pojo..
              any other idea?

              • 4. Re: EntityManager newibe Question
                dudududu Novice

                here are how I define the EM:

                 @PersistenceContext(unitName="globalEM")
                 private transient EntityManager em;
                

                here is xml
                <persistence>
                 <persistence-unit name="globalEM">
                 <provider>org.hibernate.ejb.HibernatePersistence</provider>
                 <jta-data-source>java:/DefaultDS</jta-data-source>
                 <properties>
                 <property name="hibernate.hbm2ddl.auto" value="validate"/>
                 <property name="hibernate.show_sql" value="true"/>
                 <!-- These are the default for JBoss EJB3, but not for HEM: -->
                 <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
                 <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                 <property name="jboss.entity.manager.factory.jndi.name" value="java:/selectItemsEntityManagerFactory"/>
                 <property name="jboss.entity.manager.jndi.name" value="java:/selectItemsEntityManager"/>
                 </properties>
                 </persistence-unit>
                </persistence>

                I am getting null pointer exception
                while using em.find(...)



                • 5. Re: EntityManager newibe Question
                  Rob Jellinghaus Novice

                  @PersistenceContext won't work unless your class is @Stateful or @Stateless, AFAIK.

                  Why are you not using a Seam component rather than a POJO? Seam's whole mission in life is managing your persistence context across multiple requests, conversations, sessions, etc. In similar cases in my app, I have Seam components that inject the EntityManager, and then I explicitly pass it into any POJOs that need it. This ensures I'm using the correctly scoped EntityManager for the current state of my Seam app.

                  Cheers,
                  Rob

                  • 6. Re: EntityManager newibe Question
                    hdslkf hklsdg Apprentice

                     

                    Seam's whole mission in life is managing your persistence context across multiple...


                    Wait? Do you want to say that the Seam Managed Persistence Context is the same for ALL components?

                    That would be a difference to @PersistenceContext(type=EXTENDED) as this one injects a unique PC for every bean.

                    • 7. Re: EntityManager newibe Question
                      Gavin King Master

                      Use a JNDI lookup to get a container-managed EM, or use Component.getInstance() to get a Seam-managed EM.

                      • 8. Re: EntityManager newibe Question
                        dudududu Novice

                        what are the differences between container managed and seam managed?

                        • 9. Re: EntityManager newibe Question
                          Gavin King Master

                          check the docs