简体   繁体   中英

Google App Engine: java.lang.ClassCastException: com.google.appengine.api.datastore.Key cannot be cast to java.lang.Integer

I have upgraded my GAE server application with the latest GAE SDK 1.9.17 and it stopped work. I am facing a problem while performing 'entityManager.find(Profile.class, 'some long value representing profile id')'.

Here is a stack trace:

java.lang.ClassCastException: com.google.appengine.api.datastore.Key cannot be cast to java.lang.Integer
    at org.datanucleus.store.appengine.DatastoreFieldManager.fetchIntField(DatastoreFieldManager.java:435)
    at org.datanucleus.state.AbstractStateManager.replacingIntField(AbstractStateManager.java:1132)
    at xxx.Profile.jdoReplaceField(Profile.java)
    at xxx.Profile.jdoReplaceFields(Profile.java)
    at org.datanucleus.state.JDOStateManagerImpl.replaceFields(JDOStateManagerImpl.java:2772)
    at org.datanucleus.state.JDOStateManagerImpl.replaceFields(JDOStateManagerImpl.java:2791)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.fetchObject(DatastorePersistenceHandler.java:519)
    at org.datanucleus.state.JDOStateManagerImpl.validate(JDOStateManagerImpl.java:4263)
    at org.datanucleus.ObjectManagerImpl.findObject(ObjectManagerImpl.java:2444)
    at org.datanucleus.jpa.EntityManagerImpl.find(EntityManagerImpl.java:234)
    at org.datanucleus.store.appengine.jpa.DatastoreEntityManager.find(DatastoreEntityManager.java:56)
    at xxx.GameAdd$1.run(GameAdd.java:34)
    at xxx.GameAdd$1.run(GameAdd.java:29)
    at xxx.Action.execute(Action.java:112)
    at xxx.GameAdd.doPost(GameAdd.java:28)
    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.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
    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 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.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:254)
    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.headerComplete(HttpConnection.java:923)
    at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:484)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:438)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:445)
    at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:220)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:309)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:301)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:442)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
    at java.lang.Thread.run(Thread.java:724)

The entity I find is look like:

@Entity
public class Profile
{
  public Key getKey() {return key;}

  // Other public methods 

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Key key;

  // Other data fields
}

The problem started right after I've upgraded the GAE SDK. Code wasn't changed. I always did searches using 'long' values that represent profile id.

I tried to use KeyFactory.createKey() method to generate a key for find method but it didn't help either.

Example:

Key key = KeyFactory.createKey(Profile.class.getSimpleName(), 12345);

Profile p = entitymanager.find(Profile.class, key);

I would appreciate any advises since my server application is unavailable and can't serve end users.

Thanks in advance!

Key key = KeyFactory.createKey(Profile.class.getSimpleName(), 12345);

12345 is considered an Int...

Try this:

long longVal = 12345;

Key key = KeyFactory.createKey(Profile.class.getSimpleName(), longVal);

===---===

Let me know if that helps you.

There is a chance of a problem in your build environment, especially if you have just upgraded to the new version and everything did work before. Try to use another tool to build the application, eg ant. You can find directions here . It is fairly easy and quick to make it work and it should give you an indication whether the problem is indeed in the new IntelliJ GAE support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM