首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Objectify4订单?

Objectify4订单?
EN

Stack Overflow用户
提问于 2012-12-28 15:51:18
回答 1查看 192关注 0票数 0

我升级了我的应用程序以使用Objectify4,但是我不能让排序get正常工作。这是我所做的:我有一个我想查询的课程。这个类是从Mail和Model扩展而来的。order的属性应该是在Mail-Class中索引的datetime。

代码语言:javascript
复制
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();
    }
}

这就是我想要做的:

代码语言:javascript
复制
Query<Offer> result = DatabaseService.get().load().type(Offer.class).order("-datetime");

不幸的是,结果总是不排序的。

有谁能给点提示吗?

EN

回答 1

Stack Overflow用户

发布于 2012-12-29 02:38:44

在低层,这个加载操作有两个部分:

  • filter by ^i = Offer
  • order by datetime desc

为了使其工作,您将需要一个多属性索引,如下所示:

代码语言:javascript
复制
<datastore-index kind="Model" ancestor="false">
    <property name="^i" direction="asc"/>
    <property name="datetime" direction="desc"/>
</datastore-index>

但是,通过使所有实体都扩展多态模型,您几乎可以肯定是在滥用数据存储。如果您试图将所有实体都塞进一种类型中,那么将来会遇到很多问题;首先,实际上每个查询都需要一个包含鉴别器的多属性索引。

你可以有一个通用的基类,但不要让它成为那种。保持继承层次结构,但将@Entity上移到(比如) Mail。如果你想要一个真正的多态层次结构,Offer仍然可以有@EntitySubclass。

仔细阅读objectify Concepts文档,仔细挑选您的类型。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14066381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档