Is it possible to have a computed property on Google App Engine using Java?

廉价感情. 提交于 2019-12-03 23:07:21

问题


I have an app engine application and I want to run a query that sorts the result based on a expression involving two properties. Best way I thought of doing it so far is to create a computed/calculated property that stores the result of that expression. Although I saw that GAE in Python offers a ComputedProperty, which seems to be exactly what I'm looking for, I couldn't find an equivalent in Java.

I'm currently using Objectify too, if that helps.

Any ideas?


回答1:


Compute your value in an @OnSave method:

@Entity
public class YourEntity {
    @Id Long id;

    String foo;
    String bar;

    @Index String computed;

    @OnSave void computeComputed() {
        computed = // synthesize from foo and bar
    }
}

This is what NDB's ComputedProperty actually does. Java doesn't really have a way of matching that syntax, and I'm not sure NDB's approach is any more elegant. Just leave off the setter method for computed.




回答2:


You can create an index which involves multiple properties. Something like this:

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>

Read more about it here: https://cloud.google.com/appengine/docs/java/config/indexconfig



来源:https://stackoverflow.com/questions/27843685/is-it-possible-to-have-a-computed-property-on-google-app-engine-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!