-1

I am using ActiveAndroid for the first time.

I have users that have products. I want to select all the users that have a certain product. Here is my "scheme"

@Table(name = "users")
class User extends Model {

}

@Table(name = "products")
class Product extends Model {

    @Column(name = "product_name")
    private String name;

    @Column(name = "user")
    private User user;
}

I have the following method:

public List<User> getAllUsersThatHaveProduct(String productName) {

    // What do I do here?
}

If I at least knew what tables does Active Android create and how does it relate both tables, I could have tried something, but I dont know that.

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

1 Answers1

2

You probably want to read this here: https://github.com/pardom/ActiveAndroid/wiki/Querying-the-database

Basically ActiveAndroid will create a table named "products" which will have a column "user" which will be a foreign key to the "users" table. so The id from the users record will correspond with the "user" column on "products".

so you should be able to do something like:

new Select("u.*")
    .from(User.class).as("u")
    .join(Product.class).as("p")
        .on("u.Id = p.user")
    .where("p.product_name = ?", productName)
    .execute();
laochiv
  • 2,433
  • 1
  • 15
  • 17