سؤال

I was wondering, if there is a way to sort a query using 2 columns not just one. I'm pretty sure query.order("param1").order("param2") ignores one param.

Is it possible to sort using objectify with multiple columns?

Here is my @Entity

@Entity
public class Operation {    
    @Id protected Long id;
    @Index protected Long creatorId;
    @Index protected Long categoryId;
    @Index protected Double value;
    @Index protected String description;
    @Index protected Date date;
}
هل كانت مفيدة؟

المحلول

It is possible to sort using objectify with multiple columns. In fact, the query you have in your question should work if you have a correct multi-property index.

For example, if you want to order the results by date and creatorId (taken these fields from your quetion), you need to have following index in your project's "datastore-indexes.xml" file:

<datastore-index kind="Operation " ancestor="false">
    <property name="date " direction="asc" />
    <property name="creatorId" direction="asc" />
</datastore-index>

Please note that this is just an example. Actual values for "ancestor", "direction" depends on your requirement.

Another option is to auto generate the required indexes. If you run your application on GAE development server, development server auto generates datastore index file (WEB-INF/appengine-generated/datastore-indexes-auto.xml) with required indexes.

You can read more about it here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top