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