TomEE + EclipseLink:EntityManager注入不起作用

时间:2012-05-21 23:24:26

标签: tomcat jpa ejb eclipselink apache-tomee

我正在尝试在TomEE Web Profile中运行此JPA + EJB + Facelets example。 因为示例使用EclipseLink,所以我下载了EclipseLink并将eclipselink/jlib/eclipselink.jar复制到TomEE/lib目录中。

问题1:这是我需要复制的唯一文件吗?

我有许多Entity类,一个用于处理Web请求的JSF SessionScoped ManagedBean,一个用于处理数据库请求的Stateful EJB,以及一个在启动时将数据加载到数据库中的EJB。

ManagedBean和Stateful EJB的代码片段如下:

@Singleton
@Startup
public class ConfigBean {
    @EJB
    private RequestBean request;

    @PostConstruct
    public void createData() {
        request.createPart("1234-5678-01", 1, "ABC PART", new java.util.Date(),
                "PARTQWERTYUIOPASXDCFVGBHNJMKL", null);    
        // more code here...
    }
}

@Stateful
public class RequestBean {
    private static Logger logger = Logger.getLogger("order.ejb.RequestBean");
    @PersistenceContext
    private EntityManager em;

    public void createPart(String partNumber, int revision, String description,
            java.util.Date revisionDate, String specification,
            Serializable drawing) {
        try {
            Part part = new Part(partNumber, revision, description,
                    revisionDate, specification, drawing);
            em.persist(part); // <--- EXCEPTION THROWN HERE
        } catch (Exception ex) {
            throw new EJBException(ex.getMessage());
        }
    }
}

当RequestBean中的行em.persist(part);尝试执行时,我得到以下异常:

WARNING: Injection: No such property 'order.ejb.RequestBean/em' in class order.ejb.RequestBean
May 22, 2012 12:38:39 AM org.apache.openejb.core.transaction.EjbTransactionUtil handleSystemException
SEVERE: The bean instance business method encountered a system exception: The transaction has been marked rollback only because the bean encountered a non-application exception :javax.ejb.EJBException : null
javax.ejb.EJBTransactionRolledbackException: The transaction has been marked rollback only because the bean encountered a non-application exception :javax.ejb.EJBException : null
    at org.apache.openejb.core.ivm.BaseEjbProxyHandler.convertException(BaseEjbProxyHandler.java:350)
    at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:288)
    at order.ejb.RequestBean$LocalBeanProxy.createPart(order/ejb/RequestBean.java)
    at order.ejb.ConfigBean.createData(ConfigBean.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:181)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$LifecycleInvocation.invoke(ReflectionInvocationContext.java:213)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:163)
    at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:176)
    at org.apache.openejb.monitoring.StatsInterceptor.PostConstruct(StatsInterceptor.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:181)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:163)
    at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:138)
    at org.apache.openejb.BeanContext.newInstance(BeanContext.java:1368)
    at org.apache.openejb.core.singleton.SingletonInstanceManager.createInstance(SingletonInstanceManager.java:174)
    at org.apache.openejb.core.singleton.SingletonInstanceManager.access$100(SingletonInstanceManager.java:66)
    at org.apache.openejb.core.singleton.SingletonInstanceManager$1.call(SingletonInstanceManager.java:115)
    at org.apache.openejb.core.singleton.SingletonInstanceManager$1.call(SingletonInstanceManager.java:113)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at org.apache.openejb.core.singleton.SingletonInstanceManager.getInstance(SingletonInstanceManager.java:124)
    at org.apache.openejb.core.singleton.SingletonInstanceManager.initialize(SingletonInstanceManager.java:90)
    at org.apache.openejb.core.singleton.SingletonInstanceManager.start(SingletonInstanceManager.java:81)
    at org.apache.openejb.core.singleton.SingletonContainer.start(SingletonContainer.java:118)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:795)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:506)
    at org.apache.tomee.catalina.TomcatWebAppBuilder.startInternal(TomcatWebAppBuilder.java:634)
    at org.apache.tomee.catalina.TomcatWebAppBuilder.configureStart(TomcatWebAppBuilder.java:588)
    at org.apache.tomee.catalina.GlobalListenerSupport.lifecycleEvent(GlobalListenerSupport.java:117)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.ejb.EJBException
    at order.ejb.RequestBean.createPart(RequestBean.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:181)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:163)
    at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:176)
    at org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:95)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:181)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:163)
    at org.apache.openejb.cdi.CdiInterceptor.invoke(CdiInterceptor.java:129)
    at org.apache.openejb.cdi.CdiInterceptor.access$000(CdiInterceptor.java:45)
    at org.apache.openejb.cdi.CdiInterceptor$1.call(CdiInterceptor.java:66)
    at org.apache.openejb.cdi.CdiInterceptor.aroundInvoke(CdiInterceptor.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:181)
    at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:163)
    at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:138)
    at org.apache.openejb.core.stateful.StatefulContainer.businessMethod(StatefulContainer.java:648)
    at org.apache.openejb.core.stateful.StatefulContainer.invoke(StatefulContainer.java:353)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler.java:260)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:240)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:91)
    at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:284)
    ... 45 more

由于我对Java EE的魔力不太熟悉,我几乎不知道这里出了什么问题,但我很想知道是什么导致了堆栈跟踪中的第一行,上面:

WARNING: Injection: No such property 'order.ejb.RequestBean/em' in class order.ejb.RequestBean

问题2:由于某些原因,注册实体管理器似乎不起作用。任何人都知道为什么?

为了完整性,以下是我的persistence.xml,它位于project/src/main/webapp/classes/META-INF/目录中:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="order" transaction-type="JTA">
        <jta-data-source>jdbc/__default</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="both" />
        </properties>
    </persistence-unit>
</persistence>

源代码

如果有人想查看项目的目录结构,源代码,配置文件等,我已将其上传到github here

1 个答案:

答案 0 :(得分:0)

根据Common PersistenceProvider properties更新persistence.xml以使用OpenJPA:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="order" transaction-type="JTA">
        <jta-data-source>jdbc/__default</jta-data-source>
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings"
                value="buildSchema(foreignKeys=true,schemaAction='dropDB,add')" />
            <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>

仍然有以下例外:

INFO - Starting OpenJPA 2.2.0 
java.lang.NullPointerException 
        at order.web.OrderManager.getOrders(OrderManager.java:53) 
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
        at java.lang.reflect.Method.invoke(Method.java:601) 
        at javax.el.BeanELResolver.getValue(BeanELResolver.java:64) 
        at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:58) 
        at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:169) 
        at org.apache.el.parser.AstValue.getValue(AstValue.java:169) 
        at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) 
        at org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.getValue(ContextAwareTagValueExpression.java:96) 
        at javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:246) 
        at javax.faces.component.UIData.getValue(UIData.java:2031) 
        at javax.faces.component.UIData.createDataModel(UIData.java:1979) 
        at javax.faces.component.UIData.getDataModel(UIData.java:1956) 
        at javax.faces.component.UIData.getRowCount(UIData.java:478) 
        at org.apache.myfaces.shared.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(HtmlTableRendererBase.java:366) 
        at org.apache.myfaces.shared.renderkit.html.HtmlTableRendererBase.encodeChildren(HtmlTableRendererBase.java:214) 
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:626) 
        at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:524) 
        at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:533) 
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1908) 
        at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285) 
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59) 
        at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:116) 
        at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241) 
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:199) 
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 
        at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44) 
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) 
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) 
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
        at java.lang.Thread.run(Thread.java:722) 

显然,这是TomEE 1.0.0中已知的已在trunk中解决的错误,因此我downloaded the latest beta3 snapshot

现在一切正常。