问题
With SugarORM , I understand in a relationship I can do this
public class Book extends SugarRecord<Book> {
String name;
String ISBN;
String title;
String shortSummary;
// defining a relationship
Author author;
}
How then can i do a find on Book.class such that i can order by authors.
I have tried
Book.find(Book.class, null, null, null, "author.id desc",null);;
and
Book.find(Book.class, null, null, null, "author desc",null);;
and all these wont work
回答1:
For a simple query it is recommended to use the query builder approach, for example:
Select.from(Book.class)
.orderBy("ISBN")
.list();
Anyway remember that SugarORM is based on SQLite and in your specific case you are trying to order by a relationship. Then you should build a custom query with a join, and the order by a field of the table Author (id, name, surname, etc...depending on your purpose)
回答2:
My suggestion is that you use Sugar ORM Custom Query Builder
, which can be called by using this
Book.executeQuery("VACUUM");
Or you can also use method findWithQuery
with the following syntax
List<Note> notes = Note.findWithQuery(Note.class, "SELECT * FROM Book
ORDER BY author DESC", null);
来源:https://stackoverflow.com/questions/32565108/sugarorm-orderby-related-table