EntityManager注入不能与Tomcat + OpenEJB一起使用

时间:2012-08-20 19:46:16

标签: tomcat netbeans jsf-2 openejb

我正在使用NetBeans IDE 7.1,Apache Tomcat 6.0.26,OpenEJB 3.0和JSF 2.0。首先,我将openejb.war插入文件夹: Apache Tomcat 6.0.26 \ webapps 。看起来Tomcat服务器工作正常,因为在运行项目后Tomcat服务器记录:

Apache OpenEJB 3.0    build: 20080408-04:13
http://openejb.apache.org/

这是我的代码:

GenderManager

@Stateless
public final class GenderManager {
    private final static String QUERY_GET_GENDER = "from Gender gender";
    @PersistenceContext(unitName="ExampleWebPU", name="ExampleWebPU")
    private EntityManager em;
     public List<Gender> getAllGender() {
        try {
            Query query = em.createQuery(QUERY_GET_GENDER);
            return query.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
}

GenderController

@ManagedBean
@RequestScoped
public class GenderController {
    @EJB
    private GenderManager gm;  
    public List<Gender> getAllGender() {
        return gm.getAllGender();
    }
}

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
  <persistence-unit name="ExampleWebPU" transaction-type="JTA">
    <jta-data-source>java:openejb/Resource/db1</jta-data-source>
    <non-jta-data-source>java:openejb/Resource/db1</non-jta-data-source>
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <properties>
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(foreignKeys=true" />
            <property name="openjpa.jdbc.SchemaFactory" value="native(foreignKeys=true)" />
            <property name="openjpa.jdbc.MappingDefaults"
                value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict" />
            <property name="openjpa.Log" value="DefaultLevel=TRACE,SQL=TRACE" />
    </properties>
  </persistence-unit>
</persistence>

openejb.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<Context>
  <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.0\.0\.1,0:0:0:0:0:0:0:1(%.*)?" deny=""/>
   <openejb>
    <Resource id="db1" type="DataSource">
      JdbcDriver com.mysql.jdbc.Driver
      JdbcUrl jdbc:mysql://localhost:3306/example_db1
      UserName root
      Password root
      JtaManaged true
    </Resource>
    <Resource id="db2" type="DataSource">
      JdbcDriver com.mysql.jdbc.Driver
      JdbcUrl jdbc:mysql://localhost:3306/example_db2
      UserName root
      Password root
      JtaManaged true
    </Resource>
  </openejb>
</Context>

我没有添加额外的代码。当gm.getAllGender()中的行GenderController尝试执行时,我会收到以下异常:

javax.el.ELException: /register.xhtml @50,119 value="#{genderController.allGender}": Error reading 'allGender' en el tipo com.example.GenderController
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UISelectItems.getValue(UISelectItems.java:129)
    at com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:202)
    at com.sun.faces.renderkit.SelectItemsIterator.hasNext(SelectItemsIterator.java:135)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:762)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:844)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:298)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
    at com.example.GenderController.getAllGender(GenderController.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:62)

这是如何引起的?如何解决?

2 个答案:

答案 0 :(得分:1)

不应将EJB类声明为final。我想知道你是如何编译GenderManager Stateless类的。 从final类定义中删除GenderManager

答案 1 :(得分:0)

您使用的OpenEJB版本已有4年历史。我不认为我们在版本3.1之前添加了JSF支持 - 如果我没记错的话。

我真的建议您尝试最新版本的Tomcat / OpenEJB堆栈,TomEE http://openejb.apache.org/download.html

我们基本上使用了您正在使用的代码并完成了所有功能,并通过Java EE 6 TCK获得并正式认证。我们更改了名称,称为“Tomcat / OpenEJB”对于经过认证的服务器有点误导。但是它们都是相同的开发人员和相同的代码(更好的代码版本......也许更好的开发人员版本:)。