[英]Realm: How to query with many to many field
I recently heard about Realm for android (also available for iOS) in this talk by Joaquim Verques . 我最近在Joaquim Verques的演讲中听说过Realm for android(也可用于iOS)。 Very interesting and very powerful tool for persistent data.
非常有趣且非常强大的持久数据工具。
I decided to give it a try after the video, researching and reading the documentation. 我决定在视频之后试一试,研究和阅读文档。
I found it very easy to use and set up but i end up stuck in the middle of my project because i couldn't manage to successfully make a query with many to many relationships. 我发现它很容易使用和设置,但我最终陷入了我的项目中间,因为我无法成功地进行多对多关系的查询。
I have created a small example for this topic. 我为这个主题创建了一个小例子。
My models: 我的模特:
public class Feed extends RealmObject {
@PrimaryKey
private int id;
private String title;
private String description;
private String link;
private Terms terms;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Terms getTerms() {
return terms;
}
public void setTerms(Terms terms) {
this.terms = terms;
}
}
public class Terms extends RealmObject {
private String tag;
private RealmList<Category> categories;
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public RealmList<Category> getCategories() {
return categories;
}
public void setCategories(RealmList<Category> categories) {
this.categories = categories;
}
}
public class Category extends RealmObject {
@PrimaryKey
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
So far so good, now im going to save a list of feeds to realm (make it persistent) and then try to make some query. 到目前为止一切都那么好,现在我要将一个feed列表保存到领域(让它持久化),然后尝试进行一些查询。
public static void test(final Context context,final List<Feed> feedList) {
Realm realm = null;
try {
realm = Realm.getInstance(context);
realm.beginTransaction();
realm.copyToRealmOrUpdate(feedList);
realm.commitTransaction();
RealmResults<Feed> realmResults = realm.where(Feed.class).findAll();// return me all feeds
RealmResults<Feed> realmResults1 = realm.where(Feed.class).equalTo("id", 1).findAll(); // return feed with id 1
RealmResults<Feed> realmResults2 = realm.where(Feed.class).equalTo("terms.tag", "tech").findAll(); // return feeds //with tag = "tech"
RealmResults<Feed> realmResults3 = realm.where(Feed.class).equalTo("terms.category.name", "test").findAll(); //exception here
}
} catch (Exception e) {
//Empty
Log.d("Test","",e);
} finally {
if (realm != null)
realm.close();
}
}
}
Everything run well untill the last query and i get this exception:"java.lang.IllegalArgumentException: Invalid query: category does not refer to a class." 一切运行良好,直到最后一个查询,我得到这个异常:“java.lang.IllegalArgumentException:无效的查询:类别不引用类。”
So my question is How do i do that kind of query successfully, as i will like realm to return me every feed with terms which has at least 1 category with name = "test" 所以我的问题是我如何成功地进行这种查询,因为我希望领域能够返回每个包含至少1个类别名称=“test”的条款的Feed
Thank you very much 非常感谢你
Your field is called "categories" and not "category". 您的字段称为“类别”而不是“类别”。 The third query should be:
第三个查询应该是:
RealmResults<Feed> realmResults3 = realm.where(Feed.class).equalTo("terms.categories.name", "test").findAll();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.