EJB EntityManager异常

时间:2015-09-10 07:41:33

标签: jpa ejb jpa-2.0 ejb-3.0

我在持久性

中定义了以下内容
<?xml version="1.0" encoding="UTF-8" ?>
<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" version="1.0"
             xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="test" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/testDS</jta-data-source>
    <class>test.entity.Employees</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.target-server" value="OC4J"/>
      <property name="javax.persistence.target-database" value="Oracle"/>
      <property name="javax.persistence.logging.parameters" value="TRUE"/>
    </properties>
  </persistence-unit>
</persistence>

我正在调用EntityManager的Java代码片段,我得到了异常。 我该如何解决这个问题?

使用非jta数据源,我可以使用REST检索数据,CriteriaBuilder确实可以正常工作

代码段

@Stateless(name="SessionEJB2")
@Path("/mypath")
public class SessionEJB2Bean implements SessionEJB2{
    @PersistenceContext(unitName="test")
    private EntityManager em;
    .......
    .......

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); // getting excpetion 

异常堆栈

The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
    java.lang.NullPointerException
    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 com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:734)
    at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:391)
    at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:908)
    at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:458)
    at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:226)
    at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:127)
    at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:116)
    at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
    at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
    at java.lang.Thread.run(Thread.java:662)

1 个答案:

答案 0 :(得分:2)

在应用程序中嵌入JAX-RS库时,无法在JAX-RS类中使用EE注入注释。只有在使用已将JAX-RS容器与EJB,CDI或托管bean集成的应用程序服务器中使用JAX-RS时才可能。

为了在应用程序中嵌入JAX-RS库时使用容器管理的JPA,您必须在WAR中声明JPA资源(web.xml或作为侦听器,过滤器或类的级别注释)应用程序中的其他一些servlet,然后使用JNDI访问容器管理的JPA资源。请注意,您的应用程序服务器必须支持容器管理的JPA,否则此方法也不起作用,您需要使用应用程序管理的JPA。