8 Replies Latest reply on Jun 18, 2008 9:26 PM by jchouinard

    Use of EntityManager in Conversation Beans

    charlscross

      Sorry, how can I use the entity manager in a Conversation Scoped Bean? Or can I access to the session beans methods within a Conversation Bean?

        • 1. Re: Use of EntityManager in Conversation Beans
          cremersstijn

          You can use the entitymanager with an @In annotation.


          @In(create=true)
          private EntityManager em;


          or


          with an @PersistenceContext annotation, but i think that only works within an EJB


          @PersistenceContext(type=PersistenceContextType.EXTENDED)
          private EntityManager em;

          • 2. Re: Use of EntityManager in Conversation Beans
            charlscross

            Thank you, but I get the following:
            @In attribute requires non-null value: conversador.em
            Know why?

            • 3. Re: Use of EntityManager in Conversation Beans
              cremersstijn

              do you use (create=true) after the @In ?

              • 4. Re: Use of EntityManager in Conversation Beans
                charlscross

                Yeah, the true is that I get the same error either if I use it or not. Maybe  should I use something in components.xml or another xml file?

                • 5. Re: Use of EntityManager in Conversation Beans
                  charlscross

                  For more info here is my Bean declaration:


                  @Name("conversador")


                  @Scope(ScopeType.CONVERSATION)


                  public class Conversador implements Serializable {


                  And my em declaration:


                  @In(create=true) private EntityManager em;


                  • 6. Re: Use of EntityManager in Conversation Beans
                    andygibson.contact.andygibson.net

                    In components.xml you should see something like :


                         <persistence:managed-persistence-context name="entityManager"
                              auto-create="true"
                              persistence-unit-jndi-name="java:/yourEntityManagerFactory" />
                    



                    This ensures the entity manager is always automatically created.

                    • 7. Re: Use of EntityManager in Conversation Beans
                      charlscross

                      First of all thank you for your fast answers.
                      Yes, I've got it. I'm becoming crazy!
                      This is a test Conversation Bean I did for testing but I get the same error, maybe I'm missing something, I send to you because could be something trivial I'm missing.


                      package com.eqt.maps;



                      import java.io.Serializable;



                      import javax.persistence.EntityManager;



                      import org.jboss.seam.ScopeType;


                      import org.jboss.seam.annotations.Create;


                      import org.jboss.seam.annotations.In;


                      import org.jboss.seam.annotations.Name;



                      import org.jboss.seam.annotations.Out;


                      import org.jboss.seam.annotations.Scope;



                      @Name("converser")


                      @Scope(ScopeType.CONVERSATION)


                      public class Converser implements Serializable {


                           
                           @In(create=true,required=false)


                           private EntityManager em;


                           
                           private String cosa;
                           
                           
                           
                           @Create


                           public void begin(){


                                
                                cosa = (String)em.createNativeQuery("select screenid from screens s where s.screenid=2").getSingleResult();
                           }     


                           public String getCosa() {


                                return cosa;


                           }


                           public void setCosa(String cosa) {


                                this.cosa = cosa;


                           }
                           
                           
                      }

                      • 8. Re: Use of EntityManager in Conversation Beans
                        jchouinard

                        Well if this is still alive, from what I know you need to have the same name in your xml and bean
                        if your xml is :


                        <persistence:managed-persistence-context name="entityManager"
                                  auto-create="true"
                                  persistence-unit-jndi-name="java:/yourEntityManagerFactory" />



                        you need your entity manager like this :


                        @In
                        private EntityManager entityManager;