Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Android many-to-many relationship

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?

like image 207
Barum Rho Avatar asked Jul 15 '13 02:07

Barum Rho


People also ask

What is many-to-many relationship in SQL?

Many-to-many relationships are distinct from other relationship types because there is generally no reference to the parent entity in the child entity. Instead, create a third class to represent an associative entity (or cross-reference table) between the two entities.

How do you represent a many-to-many relationship in a table?

Instead, create a third class to represent an associative entity (or cross-reference table) between the two entities. The cross-reference table must have columns for the primary key from each entity in the many-to-many relationship represented in the table.

What is a many-to-many relationship in Salesforce?

A many-to-many relationship between two entities is a relationship where each instance of the parent entity corresponds to zero or more instances of the child entity, and vice-versa. In the music streaming app example, consider again the user-defined playlists.

What is an example of a one-to-one relationship?

Define one-to-one relationships A one-to-one relationship between two entities is a relationship where each instance of the parent entity corresponds to exactly one instance of the child entity, and vice-versa. For example, consider a music streaming app where the user has a library of songs that they own.


1 Answers

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();
like image 125
Gabriel Hernández Avatar answered Oct 23 '22 12:10

Gabriel Hernández