EntityManager注入导致NullPointerException [英] EntityManager injection results in NullPointerException

查看:450
本文介绍了EntityManager注入导致NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个Java EE(EJB + Servlets等)应用程序(请注意:我正在使用Eclipse)。

我遇到了EntityManager注入无法正常工作的问题,并且由于我的Java EE(和一般的Java)noobness而找到原因有些困难。

I'm writing my first Java EE (EJB + Servlets etc.) application (please note: I'm using Eclipse).
I'm stuck with a problem with EntityManager injection not working, and having some difficulties finding why due to my Java EE (and Java in general) noobness.

这是我的 persistence.xml file - 我认为这大多是正确的,因为我可以从JMX控制台启动HSQL数据库管理器,并且我的PUBLIC.USER表正确显示。

Here is my persistence.xml file - I think this is mostly correct, since I can launch the HSQL database manager from the JMX console and my PUBLIC.USER table shows up correctly.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
    <persistence-unit name="MyPu">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/DefaultDS</jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
    </persistence-unit>
</persistence>

这是我的servlet代码:

Here's my servlet code:

[...]
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws      
    String id = request.getParameter("username");
    String password = request.getParameter("password");

    UserManagerBean um = new UserManagerBean();
    um.register(username, password);

    RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/index.jsp");
    dispatcher.forward(request, response);
}

这是我的UserManagerBean类:

And here's my UserManagerBean Class:

//bunch of imports

import myPackage.UserManager;

public @Stateful class UserManagerBean implements UserManager {
    @PersistenceContext(unitName="MyPu")
    private EntityManager persistManager;

    public void register(String username, String password) {
        User user = new User(userame, password);
        persistManager.persist(user);
        persistManager.flush();
    }
}

persistManager.persist( user)行抛出NullPointerException。
从我自己的搜索中我了解到这种情况正在发生,因为我在UserManagerBean上调用new(),从 @PersistenceContext 注释的注入永远不会发生, persistManager永远不会被束缚。

如果是这样,很明显我错过了关于EJB正确使用的一些东西。

实例化我的bean的正确方法是什么?接口有什么用?我不完全确定我的豆子应该是有状态的还是无国籍的:\

The persistManager.persist(user) line throws a NullPointerException. From my own searching I understood that this is happening because, since I'm calling new() on UserManagerBean the injection from the @PersistenceContext annotation never takes place and persistManager never gets bound.
If so, it is evident that I'm missing something about the proper usage of EJBs.
What is the right way to instantiate my bean? What's with the interfaces? I'm not entirely sure if my bean should be stateful or stateless :\

其他信息:

我更改了我的servlet中的代码,来自

Additional info:
I changed code in my servlet, from

UserManagerBean um = new UserManagerBean();

@EJB
private UserManagerBean um;

在适当的地方。现在是NullPointer。

in the appropriate place. Now um is the NullPointer.

推荐答案

引用从非EJB3 Bean引用EJB3会话Bean 来自JBoss文档:

Quoting Referencing EJB3 Session Beans from non-EJB3 Beans from the JBoss documentation:


JBoss Application Server 4.2.2通过作为运行的EJB MBean容器实现EJB3功能JBoss Application Server中的插件。这对应用程序开发有一定的影响。

JBoss Application Server 4.2.2 implemented EJB3 functionality by way of an EJB MBean container running as a plugin in the JBoss Application Server. This had certain implications for application development.

EJB3插件将EntityManager和@EJB引用的引用从一个EJB对象注入另一个EJB对象。但是,此支持仅限于EJB3 MBean及其管理的JAR文件。从WAR加载的任何JAR文件(例如Servlet,JSF支持bean等)都不会进行此处理。 Java 5企业版标准规定Servlet可以通过@EJB注释引用引用会话Bean,这在JBoss Application Server 4.2.2中没有实现。

The EJB3 plugin injects references to an EntityManager and @EJB references from one EJB object to another. However this support is limited to the EJB3 MBean and the JAR files it manages. Any JAR files which are loaded from a WAR (such as Servlets, JSF backing beans, and so forth) do not undergo this processing. The Java 5 Enterprise Edition standard specifies that a Servlet can reference a Session Bean through an @EJB annotated reference, this was not implemented in JBoss Application Server 4.2.2.

因此,即使Java EE 5规范规定了更广泛的 @EJB 注释,JBoss 4.2.2也没有不支持它。事实上,JBoss 4.2.2是一个过渡版本,并未声明完全符合Java EE 5标准。

So even though the Java EE 5 specification stipulates a wider scope of the @EJB annotation, JBoss 4.2.2 doesn't support it. In fact JBoss 4.2.2 is a transitional version that doesn't claim full Java EE 5 compliance.

因此,要么:


  • 坚持使用JBoss的实际版本,但使用JDNI查找来获取你的bean - 或 -

  • Swith到完全支持的JBoss 5 AS整个Java 5企业版规范(但具有可怕的启动性能) - 或 -

  • Swith到另一个Java EE 5应用服务器,如GlassFish v2(甚至v3)或WebLogic 10。 d使用GFv3。

这篇关于EntityManager注入导致NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆