【问题标题】:Objectify entity model design实体化实体模型设计
【发布时间】:2016-09-27 10:23:45
【问题描述】:

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

     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>

谢谢。

【问题讨论】:

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


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

    【讨论】:

    • 非常感谢。在我发布问题后想通了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    相关资源
    最近更新 更多