SugarOrm: OrderBy related table

别说谁变了你拦得住时间么 提交于 2019-12-23 08:58:13

问题


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

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