12 Replies Latest reply on Mar 31, 2008 9:29 PM by benmoore

    problem injecting POJOs

    benmoore

      Hi,


      I can inject EJBs just fine, but injecting POJOs doesn't work (the injected variables are always null). How can I go about debugging this?


      thanks, ben

        • 1. Re: problem injecting POJOs
          nickarls

          Show code (both recieving end and pojo to be injected)

          • 2. Re: problem injecting POJOs
            cleverswine

            Hi Nicklas,


            Clarification: it's only EntityManager instances that don't get injected into POJOs. Here is the code:


            @Name("authenticator")
            public class Authenticator implements Serializable {
            
              @In
              private EntityManager entityManager; // does not inject
            
              @Logger
              private Log log; // injects OK
              
              public boolean authenticate() {
                log.debug("hello world");
                List<Users> users = entityManager.createQuery("select u from Users u").getResultList();
                return true;
              }
            }



            entityManager is null in the above code; log is non-null.


            Calling code:


            <h:form>
                <h:outputLabel for="name" value="#{messages.username}"/>
                <h:inputText id="name" value="#{identity.username}"/>
                <h:outputLabel for="password" value="#{messages.password}"/>
                <h:inputSecret id="password" value="#{identity.password}"/>
                <h:commandButton value="Login" action="#{identity.login}"/>
            </h:form>



            Relevant components.xml part:


            <security:identity authenticate-method="#{authenticator.authenticate}"/>



            Injecting session EJBs with @PersistenceContext works fine. I am using Websphere 6.1. What is responsible for injected EntityManager into non-EJBs?


            thanks for any advice, ben

            • 3. Re: problem injecting POJOs
              nickarls

              Show your components.xml, persistence.xml and datasource xml

              • 4. Re: problem injecting POJOs
                dan.j.allen

                Recall that Seam uses name-based injections. Therefore, your components.xml should have an EntityManager configuration with the name entityManager. Otherwise, the injection will be null. Basically, make sure you have 1) an EntityManager component configured in components.xml and 2) the name of the component is the same as the field into which it is being injected.

                • 5. Re: problem injecting POJOs
                  shane.bryzak

                  and 3) make sure that auto-create is set to true.



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


                  • 6. Re: problem injecting POJOs
                    benmoore

                    I've made sure these 3 suggestions are there and correct. Still null. I think part of the problem is I'm using Websphere, not JBoss, and so         


                    <property name="jboss.entity.manager.factory.jndi.name"
                                       value="java:/myEntityManagerFactory" />



                    in persistence.xml does not get the EM factory into JNDI. How do people get the factory into JNDI in non-JBoss servers?


                    Nicklas:


                    components.xml:
                    core:init jndi-pattern="@jndiPattern@" debug="@debug@"/>
                    
                    <core:manager conversation-timeout="120000"                   concurrent-request-timeout="500"
                    conversation-id-parameter="cid"/>
                    
                    <persistence:managed-persistence-context name="entityManager"
                    auto-create="true" persistence-unit-jndi-name="myapp/EMFactory"/>




                    persistence.xml:
                    <persistence-unit name="myDB">
                          <provider>org.hibernate.ejb.HibernatePersistence</provider>
                          <jta-data-source>jdbc/myDatabase</jta-data-source>
                          <properties>
                            <property name="hibernate.show_sql" value="true"/>
                            <property name="hibernate.cache.provider_class"
                         value="org.hibernate.cache.HashtableCacheProvider"/>
                            <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
                            <property name="hibernate.transaction.manager_lookup_class"               value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup"/>
                          </properties>
                    </persistence-unit>



                    Again, database access is working fine from EJBs. It's only non-EJBs which have the injection problem.

                    • 7. Re: problem injecting POJOs
                      jensaug.jens.augustsson.eu

                      To debug injection:


                      <category name="org.jboss.seam.Component">
                        <priority value="TRACE" />
                      </category>



                      ...assuming you AS uses log4j.


                      • 8. Re: problem injecting POJOs
                        pmuir

                        Ben Moore wrote on Mar 31, 2008 03:41 PM:


                        How do people get the factory into JNDI in non-JBoss servers?



                        Normally using


                        <persistence:entity-manager-factory name="myPersistenceUnitName"/>



                        in components.xml

                        • 9. Re: problem injecting POJOs
                          benmoore

                          Pete Muir wrote on Mar 31, 2008 05:43 PM:


                          Normally using

                          <persistence:entity-manager-factory name="myPersistenceUnitName"/>



                          in components.xml


                          I think you meant

                          <persistence:entity-manager-factory persistence-unit-name="myPersistenceUnitName"/>



                          Indeed, that works. I thought that entity-manager-factory was optional: many of the example applications don't use it. Thanks for the help.

                          • 10. Re: problem injecting POJOs
                            pmuir

                            I did.


                            You don't need it on JBoss :-) and most examples target JBoss.

                            • 11. Re: problem injecting POJOs
                              jbalunas.jbalunas.jboss.org

                              If you look at the JPA WebSphere example it injects entity managers into POJO's.  It uses the approach that Pete talks about.


                              Is that what you are saying is not getting set or set to null?

                              • 12. Re: problem injecting POJOs
                                benmoore

                                Jay Balunas wrote on Mar 31, 2008 08:36 PM:


                                If you look at the JPA WebSphere example it injects entity managers into POJO's.  It uses the approach that Pete talks about.

                                Is that what you are saying is not getting set or set to null?



                                Yes, that is what is null, unless <persistence:entity-manager-factory/> is specified. I think my confusion is due to the Chapter 28 documentation. Although Chatper 28 walks you through how to convert the jee/booking sample to work with Websphere, that sample doesn't have any non-session EJB Seam components. Yes, jpa/booking has non-session EJB Seam components, but it doesn't have any session EJB components.


                                In other words, there isn't a sample that has both session EJBs and non-session EJBs defined as Seam components... at least for booking and many of the others (haven't checked all the samples).


                                Is there any reason the two shouldn't be commingled into the same application? Or, to rephrase: when should I choose to make my Seam components session EJBs vs. non-session EJBs?


                                Thank you for the guidance!