我升级了我的应用程序以使用Objectify4,但是我不能让排序get正常工作。这是我所做的:我有一个我想查询的课程。这个类是从Mail和Model扩展而来的。order的属性应该是在Mail-Class中索引的datetime。
import com.googlecode.objectify.annotation.EntitySubclass;
import com.googlecode.objectify.annotation.Serialize;
@EntitySubclass(index=true)
public class Offer extends Mail {
private static final long serialVersionUID = -6210617753276086669L;
@Serialize private Article debit;
@Serialize private Article credit;
private boolean accepted;
...
}
import com.googlecode.objectify.annotation.EntitySubclass;
import com.googlecode.objectify.annotation.Index;
@EntitySubclass(index=true)
public class Mail extends Model {
private static final long serialVersionUID = 8417328804276215057L;
@Index private Long datetime;
@Index private String sender;
@Index private String receiver;
...}
import java.io.Serializable;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Ignore;
import com.googlecode.objectify.annotation.Index;
@Entity
public class Model implements Serializable {
private static final long serialVersionUID = -5821221296324663253L;
@Id Long id;
@Index String name;
@Ignore transient private Model parent;
@Ignore transient private boolean changed;
...}
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
public class DatabaseService {
static {
ObjectifyService.register(Model.class);
ObjectifyService.register(Mail.class);
ObjectifyService.register(Offer.class);
}
public static Objectify get() {
return ObjectifyService.ofy();
}
}这就是我想要做的:
Query<Offer> result = DatabaseService.get().load().type(Offer.class).order("-datetime");不幸的是,结果总是不排序的。
有谁能给点提示吗?
发布于 2012-12-29 02:38:44
在低层,这个加载操作有两个部分:
为了使其工作,您将需要一个多属性索引,如下所示:
<datastore-index kind="Model" ancestor="false">
<property name="^i" direction="asc"/>
<property name="datetime" direction="desc"/>
</datastore-index>但是,通过使所有实体都扩展多态模型,您几乎可以肯定是在滥用数据存储。如果您试图将所有实体都塞进一种类型中,那么将来会遇到很多问题;首先,实际上每个查询都需要一个包含鉴别器的多属性索引。
你可以有一个通用的基类,但不要让它成为那种。保持继承层次结构,但将@Entity上移到(比如) Mail。如果你想要一个真正的多态层次结构,Offer仍然可以有@EntitySubclass。
仔细阅读objectify Concepts文档,仔细挑选您的类型。
https://stackoverflow.com/questions/14066381
复制相似问题