Why I get an casting error when I try to get some database object using com.google.appengine.api.datastore.Query
For example: DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query q = new Query("User");
PreparedQuery pq = datastore.prepare(q);
for (Entity entity : pq.asIterable()) {
User myUser = (User)entity;
}
You can not do that. Entities returned are of type Entity
and in java you can not just cast one type to another.
If you want queries to return objects of custom type, you need some kind of mapping framework, for example objectify
. It lets you directly use your classes in database operations.
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.