1

このJPA + EJB + Facelets の例を TomEE Web Profile で実行しようとしています。サンプルでは EclipseLink を使用しているため、EclipseLink をダウンロードeclipselink/jlib/eclipselink.jarしてTomEE/libディレクトリにコピーしました。

質問 1:コピーする必要があるファイルはこれだけですか?

いくつかのEntityクラスがあります。Web リクエストを処理する JSF SessionScoped ManagedBean が 1 つ、データベース リクエストを処理するステートフル EJB が 1 つ、起動時にデータベースにデータをロードする EJB がもう 1 つです。

ManagedBean とステートフル 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:何らかの理由で、Entity Manager のインジェクションが機能していないようです。誰でも理由がわかりますか?

完全を期すためpersistence.xmlに、project/src/main/webapp/classes/META-INF/ディレクトリにある my を以下に示します。

<?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にアップロードしました。

4

1 に答える 1

0

Common PersistenceProviderプロパティ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) 

どうやらそれはトランクで解決されたTomEE1.0.0の既知のバグなので、最新のbeta3スナップショットをダウンロードしました

これで、すべてが正常に実行されます。

于 2012-05-22T13:33:56.790 に答える