(持久性)无法创建实体管理器

时间:2013-01-25 14:28:49

标签: java servlets jpa persistence

我遇到了这个问题,2天我无法弄清楚为什么会发生这种情况。 我想验证登录。我的代码:

public String Verify(String name, String password) {
String admin="";
    Query q = emf
            .createEntityManager()
            .createQuery(
                    "SELECT u FROM user u where u.name = :name and u.password=:password");
    q.setParameter("name", name);
    q.setParameter("password", password);
    User u = (User) q.getSingleResult();

    if (u.getAdmin().equalsIgnoreCase("yes")) {
        admin = "yes";
    } else {
        admin = "no";
    }

    return admin;
}

我收到了这个错误:

java.lang.NullPointerException
at db.DbProcessor.Verify(DbProcessor.java:19)
at controller.VerifikoServlet.doGet(VerifikoServlet.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
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)

第19行是“.createEntityManager()”

3 个答案:

答案 0 :(得分:1)

我修复了写这个以初始化实体工厂管理器的问题:

private static EntityManagerFactory factory = Persistence
        .createEntityManagerFactory("PROJECT_NAME");
EntityManager em = factory.createEntityManager();

答案 1 :(得分:0)

问题是您尚未初始化EntityManagerFactory。正如我所看到的,您正在Servlet环境中运行。

查看此链接,您可能需要阅读:http://weblogs.java.net/blog/ss141213/archive/2005/12/dont_use_persis_1.html

答案 2 :(得分:0)

如果加载持久层不是这个,我们可以通过简单地添加以下代码来验证: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ public class JpaUtil {     private static final EntityManagerFactory emf;

static {
    try {
        factory = Persistence.createEntityManagerFactory("MyPu");
    } catch (Throwable ex) {
        logger.error("Initial SessionFactory creation failed", ex);
        throw new ExceptionInInitializerError(ex);
    }
}

...

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

请注意,这不是获取实体Manager Factory对象的常见内容。但是重要的是内部的异常处理,即ExceptionInInitializerError ...

这将清楚地告诉您是否存在与您的Entity类相关的任何问题,例如Date类型变量的丢失的TemporalType或类似的。

因此,如果假设您在实体类中使用NamedQuery,但即使由于实体类中的任何缺少类型或必需属性(如Date类型的TemoralType)而导致持久性无法加载它。

所以请确保您处理提到的例外