我试图让这个JPA + EJB + Facelets 示例在 TomEE Web Profile 中运行。因为该示例使用 EclipseLink,所以我下载了 EclipseLink 并复制eclipselink/jlib/eclipselink.jar
到该TomEE/lib
目录中。
问题 1:这是我需要复制的唯一文件吗?
我有许多Entity
类,一个用于服务 Web 请求的 JSF SessionScoped ManagedBean,一个用于处理数据库请求的有状态 EJB,还有一个用于在启动时将数据加载到数据库中的 EJB。
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());
}
}
}
当em.persist(part);
RequestBean 中的行尝试执行时,我收到以下异常:
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:由于某种原因,实体管理器的注入似乎不起作用。有人知道为什么吗?
为了完整起见,下面是 my 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了。