-
1. Re: problem injecting POJOs
nickarls Mar 25, 2008 7:57 PM (in response to benmoore)Show code (both recieving end and pojo to be injected)
-
2. Re: problem injecting POJOs
cleverswine Mar 28, 2008 10:24 PM (in response to benmoore)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 Mar 29, 2008 12:25 PM (in response to benmoore)Show your components.xml, persistence.xml and datasource xml
-
4. Re: problem injecting POJOs
dan.j.allen Mar 29, 2008 10:13 PM (in response to benmoore)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 Mar 31, 2008 1:14 AM (in response to benmoore)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 Mar 31, 2008 3:41 PM (in response to 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 Mar 31, 2008 4:08 PM (in response to benmoore)To debug injection:
<category name="org.jboss.seam.Component"> <priority value="TRACE" /> </category>
...assuming you AS uses log4j.
-
8. Re: problem injecting POJOs
pmuir Mar 31, 2008 5:43 PM (in response to benmoore)
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 Mar 31, 2008 8:09 PM (in response to benmoore)
Pete Muir wrote on Mar 31, 2008 05:43 PM:
Normally using<persistence:entity-manager-factory name="myPersistenceUnitName"/>
in components.xmlI 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 Mar 31, 2008 8:31 PM (in response to benmoore)I did.
You don't need it on JBoss :-) and most examples target JBoss.
-
11. Re: problem injecting POJOs
jbalunas.jbalunas.jboss.org Mar 31, 2008 8:36 PM (in response to benmoore)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 Mar 31, 2008 9:29 PM (in response to 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!