Java 使用过滤器将投影查询对象化
在课堂上Java 使用过滤器将投影查询对象化,java,google-app-engine,google-cloud-datastore,objectify,Java,Google App Engine,Google Cloud Datastore,Objectify,在课堂上 @Entity public class Goat{ Long pen; Long car; Boolean active; … } 我需要创建一个投影查询,返回Longs的列表。我应该如何编辑以下内容以使其正常工作 List<Long> cars = ofy().load().type(Goat.class).filter("pen", pen).filter("active", true).project("car").list(); Lis
@Entity
public class Goat{
Long pen;
Long car;
Boolean active;
…
}
我需要创建一个投影查询,返回Long
s的列表。我应该如何编辑以下内容以使其正常工作
List<Long> cars = ofy().load().type(Goat.class).filter("pen", pen).filter("active", true).project("car").list();
List cars=ofy().load().type(Goat.class).filter(“pen”,pen).filter(“active”,true).project(“car”).List();
您需要为所有计划查询/筛选的字段编制索引。
例如
它不编译。是否可以更改为
类型(Long.class)
?它如何知道我查询的是哪个类?另一方面,如果我不指定Long,它与预期的类型不一致。什么不编译?因为我预期List
,如果我将类型设置为type(Goat.class)
它不编译。因此,似乎期望查询的类型和结果集具有相同的类型。基本上阅读我的代码行来查看我的不匹配。或者我猜投影仍然返回Goat
,但只定义了car
。正如这个答案所述:项目不能作为SQL select语句工作,您仍然可以得到整个实体。此外,投影查询非常奇特,不应该被GAE的初学者使用。当你真正知道自己在做什么时,把它们当作一个最后的选择。
@Index
Long pen;
@Index
Long car;