SugarOrm: order related table

With SugarORM, I understand the relationship, I can do it

public class Book extends SugarRecord<Book> {
  String name;
  String ISBN;
  String title;
  String shortSummary;

  // defining a relationship
  Author author;
}

How then can I find on Book.class so that I can order by the authors.

I 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 this does not work

+4
source share
2 answers

For a simple query, it is recommended that you use the query builder approach , for example:

Select.from(Book.class)
.orderBy("ISBN")
.list();

In any case, remember that SugarORM is based on SQLite and in your particular case you are trying to streamline the relationship. Then you must create a custom query with a join and order in the field of the Author table (id, name, surname, etc.) Depending on your purpose.

+6
source

, Sugar ORM Custom Query Builder,

Book.executeQuery("VACUUM");

findWithQuery

List<Note> notes = Note.findWithQuery(Note.class, "SELECT * FROM Book 
                                        ORDER BY author DESC", null);
+3

Source: https://habr.com/ru/post/1607273/


All Articles