Appengine: ClassCastException in query with string of leng
In my Google App Engine project I got an unexpected exception:
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.lang.String org.learnjapanese.client.NewAccount.login(java.lang.String,java.lang.String) throws java.lang.IllegalArgumentException' threw an unexpected exception: java.lang.ClassCastException: java.lang.Character cannot be cast to com.google.appengine.api.datastore.Key
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:378)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:581)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:188)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:224)
This is my JPA-code:
@Entity
public class Test {
@Id
String id;
}
And this is the application code:
EntityManager manager = EMF.get().createEntityManager();
Query q = manager.createQuery("select x from Test x where id = '12'");
List l = q.getResultList(); /开发者_JAVA技巧/ No problem here
q = manager.createQuery("select x from Test x where id = '1'");
l = q.getResultList(); // java.lang.ClassCastException occurs here.
It turns out that a query with a String of length 1 causes the exception, while the String with length 2 does not. Can anyone explain what is going on here? Is there a workaround?
In Java a single character enclosed in single quotes is interpreted as a char literal. It looks like this is what's happening when you use '1'
in your statement. The stack trace is talking about an instance of java.lang.Character
which is what you would get if a char was auto-boxed. Sounds like a bug in the framework you're using.
精彩评论