2

为什么当我尝试使用 com.google.appengine.api.datastore.Query 获取某些数据库对象时出现转换错误

例如:DatastoreService 数据存储 = DatastoreServiceFactory.getDatastoreService(); 查询 q = new Query("用户");

PreparedQuery pq = datastore.prepare(q);

for (Entity entity : pq.asIterable()) {
   User myUser = (User)entity;
}
4

1 回答 1

3

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.

于 2012-05-13T18:38:41.637 回答