1

我是 GAE 开发的新手。对我来说,使用 jpa 插入数据存储区工作正常,但没有进行检索。

下面是一种在用户尝试登录时对用户进行身份验证的方法。

public boolean loginUser(User user) {

    EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("SOAApp");
    EntityManager mgr = emf.createEntityManager();
    boolean login=false;
    Query query=mgr.createQuery("select u from User u where u.userName=:uid and u.password=:pwd");

    query.setParameter("uid", user.getUserName());
    query.setParameter("pwd",user.getPassword());
    query.setMaxResults(1);

    try {
        user=(User) query.getSingleResult();
        System.out.println("user : "+user.getName());
        if(user!=null)  {
            System.out.println("logged in...");
            login=true;
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    finally {
        mgr.close();
    }
    return login;
}

但是在尝试通过传递具有有效用户名和密码的用户对象来执行该方法时出现以下错误。

java.lang.NullPointerException
at com.google.appengine.datanucleus.query.QueryEntityPKFetchFieldManager.fetchIntField(QueryEntityPKFetchFieldManager.java:70)
at org.datanucleus.identity.IdentityUtils.getApplicationIdentityForResultSetRow(IdentityUtils.java:93)
at com.google.appengine.datanucleus.EntityUtils.entityToPojo(EntityUtils.java:1009)
at com.google.appengine.datanucleus.query.DatastoreQuery$2.apply(DatastoreQuery.java:228)
at com.google.appengine.datanucleus.query.DatastoreQuery$2.apply(DatastoreQuery.java:225)
at com.google.appengine.datanucleus.query.LazyResult.resolveNext(LazyResult.java:96)
at com.google.appengine.datanucleus.query.LazyResult.resolveAll(LazyResult.java:121)
at com.google.appengine.datanucleus.query.LazyResult.size(LazyResult.java:115)
at com.google.appengine.datanucleus.query.StreamingQueryResult.size(StreamingQueryResult.java:151)
at org.datanucleus.store.query.Query.executeQuery(Query.java:1808)
at org.datanucleus.store.query.Query.executeWithMap(Query.java:1693)
at org.datanucleus.api.jpa.JPAQuery.getSingleResult(JPAQuery.java:231)
at com.soa.managers.UserManager.loginUser(UserManager.java:63)
at com.soa.servlets.LoginServlet.doPost(LoginServlet.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:61)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:383)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

2012 年 11 月 3 日下午 3:25:02 com.google.appengine.tools.development.LocalResourceFileServlet doGet

第 63 行 [在 com.soa.managers.UserManager.loginUser(UserManager.java:63)] 是“user=(User) query.getSingleResult();”

请帮我

4

4 回答 4

1

仍在搜索中?

我遇到了同样的问题,我的实体 User 有一个名为 userId 的主键,其类型为“int”。现在我将其更改为“长”,我所有的问题和警告都消失了!也许这可以帮助你!

我期待着您的回音!

问候,斯蒂芬

于 2012-11-19T21:01:03.817 回答
0

确保您的实体的密钥是此处详述的有效类型: https ://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata#Keys

您遇到的异常很可能与关键问题有关。

于 2014-01-23T18:59:17.200 回答
0

你得到nullpointerexception的原因是getSingleResult方法返回null。因为你使用了 (User)query.getSingleResult(),所以 null 值的类型转换会引发异常。

我认为数据存储区中没有相关用户,因此您遇到了异常。

于 2012-11-03T18:27:03.610 回答
0
<?xml version="1.0" encoding="UTF-8"?>
<datastore-indexes  autoGenerate="true">
    <datastore-index kind="HABERLER" ancestor="false" source="manual">
        <property name="__key__" direction="desc"/>
    </datastore-index>

HABERLER 是表名。写下你的表名而不是 HABERLER。然后在 WEB-INF 目录中创建一个 datastore-indexes.xml 文件。然后将上面的代码复制到这个文件中。

于 2012-11-04T07:45:40.037 回答