Container Managed EntityManager

时间:2015-09-16 10:46:37

标签: jpa ejb jpa-2.0 ejb-3.0 jndi

我正在尝试使用Container Managed EntityManager,但是我收到了NameNotFoundException。我尝试在web.xml中添加条目但是徒劳无功。

@Stateless
@Path("/mypath")
public class EmployeeService {

    @EJB
    private EmployeeDAO employeeDAO;

    @GET
    @Path("/myresults")
    @Produces(MediaType.APPLICATION_JSON)
    public Employee getValues() {
        Employee emp = new Employee();
        try  {
            emp = employeeDAO.getEmployees(); // exception here
        } catch (Exception ex)  {
            ex.printStackTrace();
        } 
        return emp;
    }

EmployeeDAO代码段

public class EmployeeDAO {
    @PersistenceContext(unitName = "test-ejb", name = "persistence/em")

    public Employee getEmployees() throws NamingException {
        int empNo = 342;
        Context context = new InitialContext();
        context.lookup("java:comp/env");
        EntityManager em = (EntityManager)context.lookup("persistence/em");
        return em.find(Employee.class, empNo);
    }

的persistence.xml

<?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-ejb" 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>

异常堆栈

  

javax.naming.NameNotFoundException:找不到持久性/ em       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.rmi.RMIServerContext.lookup(RMIServerContext.java:207)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.ApplicationContext.unprivileged_lookup(ApplicationContext.java:256)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.ApplicationContext.lookup(ApplicationContext.java:196)       在javax.naming.InitialContext.lookup(InitialContext.java:392)       at test.dao.EmployeeDAO.getValues(EmployeeDAO.java:39)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)       在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.Abs​​tractResourceMethodDispatchProvider $ TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)       at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)       在com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)       在com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)       在com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)       在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)       在com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)       在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)       在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:856)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:734)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:391)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:908)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:458)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:226)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.http.HttpRequestHandler.run(HttpRequestHandler.java:127)       at com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .server.http.HttpRequestHandler.run(HttpRequestHandler.java:116)       at oracle.oc4j.network.ServerSocketReadHandler $ SafeRunnable.run(ServerSocketReadHandler.java:260)       在com.evermind [Oracle Containers for J2EE 10g(10.1.3.5.0)] .util.ReleasableResourcePooledExecutor $ MyWorker.run(ReleasableResourcePooledExecutor.java:303)       在java.lang.Thread.run(Thread.java:662)

更新1

@Stateless
@LocalBean
public class EmployeeDAO {
    @PersistenceContext(unitName = "test-ejb", name = "persistence/em")

    public Employee getEmployees() throws NamingException {
        int empNo = 342;
        Context context = new InitialContext();
        context.lookup("java:comp/env");
        EntityManager em = (EntityManager)context.lookup("persistence/em");
        return em.find(Employee.class, empNo);
    }

异常

  

javax.ejb.EJBException:应用于非setter方法的setter注入:getValues;嵌套异常是:java.lang.InstantiationException:应用于非setter方法的setter注入:getValues

全栈

  

引起:java.lang.InstantiationException:应用于非setter方法的setter注入:getValues       at com.evermind.server.deployment.J2EEContextAnnotationParser.validateInjectionSetter(J2EEContextAnnotationParser.java:713)       at com.evermind.server.deployment.J2EEContextAnnotationParser.createInjectionTarget(J2EEContextAnnotationParser.java:729)       at com.evermind.server.deployment.J2EEContextAnnotationParser.parseInjectedMethod(J2EEContextAnnotationParser.java:378)       at com.evermind.server.ejb.deployment.BeanDescriptorAnnotationParser.parseInjectedMethodAndCallbacks(BeanDescriptorAnnotationParser.java:276)       at com.evermind.server.ejb.deployment.SessionBeanDescriptorAnnotationParser.parseInjectedMethodAndCallbacks(SessionBeanDescriptorAnnotationParser.java:95)       at com.evermind.server.ejb.deployment.BeanDescriptorAnnotationParser.parseAnnotations(BeanDescriptorAnnotationParser.java:77)       at com.evermind.server.ejb.deployment.BeanDescriptorAnnotationParser.parseAnnotations(BeanDescriptorAnnotationParser.java:48)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)       在java.lang.reflect.Method.invoke(Method.java:597)       at com.evermind.server.ejb.BeanAnnotationListener.parseAnnotatedClass(BeanAnnotationListener.java:45)

2 个答案:

答案 0 :(得分:1)

更改:

public class EmployeeDAO {
    @PersistenceContext(unitName = "test-ejb", name = "persistence/em")
...

为:

@PersistenceContext(unitName = "test-ejb", name = "persistence/em")
@Stateless
public class EmployeeDAO {
...

答案 1 :(得分:1)

EmployeeDAO必须是容器管理的类(侦听器,过滤器,servlet,EJB,托管bean,CDI托管bean等),而不是POJO。 JPA容器只会在容器管理的类上搜索JPA注释,而不是在POJO上搜索。

您的选择是:

  1. 将EmployeeDAO更改为容器管理的类。例如,如果您的应用程序服务器支持EJB,则将@Stateless注释添加到类中。
  2. @PersistenceContext注释移动到容器管理的类。例如,如果您有现有的侦听器,过滤器或servlet,请将注释移动到该类。
  3. <persistence-context-ref>添加到web.xml。
  4. 请注意,您的应用程序服务器必须支持容器管理的JPA才能使用这些方法。