npe when injecting entityManager using PersistenceContext
medibadge Aug 14, 2008 9:21 PMWhen I try to inject entityManager in this stateful session bean the entityManager is null. The only thing different about my component is that it is not linked to any el (unlike the examples).
The intention of my component is to read some small control tables, hopefully more efficiently than the Seam CRUD Application Framework does. I want this component to return a control table as a List to the calling class. Once I get this version running I will modify it using Generics to read all control files.
I am running jboss-4.2.2.GA and jboss-seam-2.0.2.SP1. The application was initially generated with Seam Gen.
Thanks in advance,
Dick Starr
Here is my stateful session bean :
package com.starrcs.action.utilities; import com.starrcs.action.common.*; import com.starrcs.model.system.*; import java.util.*; import javax.ejb.*; import javax.persistence.*; import org.jboss.seam.annotations.*; @Stateful @Name("ctrlTable") public class CtrlTableAction extends BaseAction implements CtrlTable { private List<SyCtrlMain> ctrlList; @PersistenceContext private EntityManager entityManager; @Remove public void destroy() { } public List<SyCtrlMain> retrieveAllCtrlList() { // Why is entityManager null here? ctrlList = entityManager.createQuery("select ctrl from SyCtrlMain ctrl").getResultList(); return ctrlList; } }
Here is the interface:
package com.starrcs.action.utilities; import com.starrcs.action.common.*; import com.starrcs.model.system.*; import java.util.*; import javax.ejb.*; @Local public interface CtrlTable extends BaseSessionBean { // Methods, in alphabetic order //public void destroy(); public List<SyCtrlMain> retrieveAllCtrlList(); }
Here is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- Persistence deployment descriptor for dev profile --> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="starraSeam"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/starraSeamDatasource</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="validate"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="jboss.entity.manager.factory.jndi.name" value="java:/starraSeamEntityManagerFactory"/> </properties> </persistence-unit> </persistence>
Here is my components.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- components.xml 2008-06-26 RJS. New. --> <components xmlns="http://jboss.com/products/seam/components" xmlns:core="http://jboss.com/products/seam/core" xmlns:persistence="http://jboss.com/products/seam/persistence" xmlns:drools="http://jboss.com/products/seam/drools" xmlns:bpm="http://jboss.com/products/seam/bpm" xmlns:security="http://jboss.com/products/seam/security" xmlns:mail="http://jboss.com/products/seam/mail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.0.xsd http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.0.xsd http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.0.xsd http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.0.xsd http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.0.xsd http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd"> <core:init debug="@debug@" jndi-pattern="@jndiPattern@"/> <core:manager concurrent-request-timeout="1000" conversation-timeout="120000" conversation-id-parameter="cid"/> <persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/starraSeamEntityManagerFactory"/> <drools:rule-base name="securityRules"> <drools:rule-files> <value>/security.drl</value> </drools:rule-files> </drools:rule-base> <security:identity security-rules="#{securityRules}" authenticate-method="#{authenticator.authenticate}" remember-me="true"/> <event type="org.jboss.seam.security.notLoggedIn"> <action execute="#{redirect.captureCurrentView}"/> </event> <event type="org.jboss.seam.security.loginSuccessful"> <action execute="#{redirect.returnToCapturedView}"/> </event> <mail:mail-session host="localhost" port="2525" username="test" password="test" /> <!-- For use with jBPM pageflow or process management --> <!-- <bpm:jbpm> <bpm:process-definitions></bpm:process-definitions> <bpm:pageflow-definitions></bpm:pageflow-definitions> </bpm:jbpm> --> <core:resource-loader> <core:bundle-names> <value>messages</value> <value>msgsApp</value> <value>msgsMenu</value> </core:bundle-names> </core:resource-loader> </components>