对象化实体模型设计

Objectify entity model design

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

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

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

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

现在当我写我的数据存储-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>

谢谢。

在 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,其中包含为这些查询提供服务所需的索引。