【问题标题】:Is it possible to have a computed property on Google App Engine using Java?是否可以使用 Java 在 Google App Engine 上拥有计算属性?
【发布时间】:2015-03-06 18:40:01
【问题描述】:

我有一个应用引擎应用程序,我想运行一个查询,该查询根据涉及两个属性的表达式对结果进行排序。到目前为止,我想到的最好方法是创建一个计算/计算属性来存储该表达式的结果。虽然我看到 Python 中的 GAE 提供了一个 ComputedProperty,这似乎正是我正在寻找的,但我在 Java 中找不到等价物。

我目前也在使用 Objectify,如果有帮助的话。

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    在 @OnSave 方法中计算您的值:

    @Entity
    public class YourEntity {
        @Id Long id;
    
        String foo;
        String bar;
    
        @Index String computed;
    
        @OnSave void computeComputed() {
            computed = // synthesize from foo and bar
        }
    }
    

    这就是 NDB 的 ComputedProperty 实际所做的。 Java 并没有真正匹配该语法的方法,而且我不确定 NDB 的方法是否更优雅。只需省略 computed 的 setter 方法即可。

    【讨论】:

      【解决方案2】:

      您可以创建涉及多个属性的索引。像这样的:

      class X{
         @Indexed public String a;
         @Indexed public String b;
      }
      
      <datastore-index kind="X" ancestor="false">
          <property name="a" direction="asc" />
          <property name="b" direction="asc" />
      </datastore-index>
      

      在此处了解更多信息:https://cloud.google.com/appengine/docs/java/config/indexconfig

      【讨论】:

      • 谢谢,但我实际上是在寻找一种方法来根据从 2 个现有属性中计算出的新属性(例如,在您的示例中为“a”和“b”)对结果进行排序。 Stickfigure 的回答对我有用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2012-05-08
      • 2010-12-26
      • 1970-01-01
      • 2011-05-09
      • 2021-06-03
      • 2013-03-25
      相关资源
      最近更新 更多