客观化实体模型设计

时间:2016-09-27 10:23:46

标签: java google-app-engine google-cloud-datastore objectify

我正在使用一种我不确定是完全正确的设计模型,所以我想也许我应该在那里提出一个问题来找出最佳方法。 我创建了一个基本实体,其中包含两个字段:

     public abstract  class BaseEntity
     @Index private Key<User> createdBy;
     @Index private DateTime creationDate = new DateTime();

现在我还有另一个名为User的子类,它有自己的索引,例如:

     @Entity
     @Cache
     public class user extends BaseEntity
     @Index private String email ;
     @Index private String dob 

现在,当我编写datastore-indexes.xml文件时,这样做是正确的:

    <datastore-index kind="User"  ancestor="false" source="manual">
    <property name="createdBy" direction="asc"/>
    <property name="creationDate" direction="asc"/>
    <property name="email" direction="asc"/>
    <property name="dob" direction="asc"/>
   </datastore-index>

      or 

     <datastore-index kind="User" ancestor="false" source="manual">
    <property name="email" direction="asc"/>
    <property name="dob" direction="asc"/>
</datastore-index>

谢谢。

1 个答案:

答案 0 :(得分:1)

在Cloud Datastore中,您需要的索引完全取决于您要运行的查询,而不是数据模型。文档中的index definition部分非常有用。例如,如果要运行查询:

Query<User> q = ofy().load().type(User.class) .filter("email", "test@example.com").order("createdDate");

你需要索引:

<datastore-index kind="User" ancestor="false" source="manual"> <property name="email" direction="asc"/> <property name="createdDate" direction="asc"/> </datastore-index>

您列出的任何索引都不满足查询(即使第二个索引中列出的属性是必要索引中列出的属性的超集)。

您应该使用App Engine development server运行您的应用程序,并运行您需要在生产服务中运行的应用程序的任何查询。这将自动生成一个datastore-indexes-auto.xml,其中包含提供这些查询所需的索引。