Active Android many-to-many relationship
Asked Answered
R

1

8

Although this question is about ActiveAndroid, anyone who is familiar with ORMs could probably answer this question.

ActiveAndroid doesn't seem to give you a way to do many-to-many relationships out of the box. What I found while searching for a solution was this GitHub issue: https://github.com/pardom/ActiveAndroid/issues/46

I understand that it's explicitly creating the relationship table, but I don't understand how the following part is supposed to do anything useful:

public List<Foo> foos() {
    return getMany(Foo.class, "FooBar");
}
public List<Bar> bars() {
    return getMany(Bar.class, "FooBar");
}

This would result in a query like SELECT * FROM Foo where Foo.FooBar = FooBar.Id;. This would return at most one Foo row. Am I missing something?

Don't you need a query involving a join?

Rubberneck answered 15/7, 2013 at 2:24 Comment(1)
Here is my answer on another place: Activeandroid Many-to-manyLauderdale
K
4

Let's say you want to select all Foos for one specific Bar, you would do this:

List<Foo> foos = ((Foo) new Select().from(FooBar.class)
                .where("Foo = ?", this.getId())
                .executeSingle()).foos();
Kingmaker answered 12/5, 2014 at 1:38 Comment(2)
Is this supposed to go in Bar.java?Junket
@Waclock, yes. And I suppose, there should be cast to FooBar.Eh

© 2022 - 2024 — McMap. All rights reserved.