[英]How to convert com.google.appengine.api.datastore.Entity to my Entity Object?
Why I get an casting error when I try to get some database object using com.google.appengine.api.datastore.Query 当我尝试使用com.google.appengine.api.datastore.Query尝试获取某个数据库对象时,为什么会出现转换错误
For example: DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 例如:DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query q = new Query("User"); 查询q =新查询(“用户”);
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. 返回的实体是Entity
类型,在java中,您不能只将一种类型转换为另一种类型。
If you want queries to return objects of custom type, you need some kind of mapping framework, for example objectify
. 如果您希望查询返回自定义类型的对象,则需要某种映射框架,例如objectify
。 It lets you directly use your classes in database operations. 它允许您直接在数据库操作中使用您的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.