Objectify4订单?

时间:2012-12-28 07:51:18

标签: java objectify

我升级了我的应用程序以使用Objectify4,但我无法让订单工作。 这是我做的: 我有一个我要查询的类优惠。此类从Mail和Model扩展。订单的属性应该是在Mail-Class中索引的日期时间。

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");

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

有人提示吗?

1 个答案:

答案 0 :(得分:1)

在低级别,此加载操作包含两部分:

  • 按^ i =提供
  • 过滤
  • 按日期时间desc排序

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

<datastore-index kind="Model" ancestor="false">
    <property name="^i" direction="asc"/>
    <property name="datetime" direction="desc"/>
</datastore-index>

但是,您几乎肯定会通过使所有实体扩展多态模型来滥用数据存储区。如果您尝试将所有实体塞入单一种类,将来会遇到许多问题;一方面,几乎每个查询都需要一个多属性索引,包括鉴别器。

你可以有一个共同的基类,只是不要使它成为善良。保留继承层次结构,但将@Entity移动到(比如说)Mail。如果你想要一个真正的多态层次结构,商品仍然可以有@EntitySubclass。

仔细阅读客观化的概念文档并仔细挑选您的种类。

相关问题