2012-12-28 30 views
0

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

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

Unfortunely,结果总是不排序。

有没有人提示?

回答

1

在低水平,该加载操作有两个部分:

  • 过滤器通过^ I =发售
  • ORDER BY日期时间降序

为了使其工作,你将需要像这样的多属性索引:

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

但是,你几乎肯定会滥用d通过让所有的实体扩展一个多态模型来达到目的。如果您尝试将所有实体都塞进一个单独的实体中,您将来会遇到许多问题;一方面,实际上每个查询都需要包含鉴别器的多属性索引。

你可以有一个共同的基类,只是不要使它成为那种。保持继承层次结构,但将@Entity移至(比如说)Mail。如果您想要一个真正的多态层次结构,Offer仍然可以拥有@EntitySubclass。

仔细阅读物品概念文档并仔细挑选您的种类。

+0

链接:https://code.google.com/p/objectify-appengine/wiki/Entities#Polymorphism – konqi