N
N
newdancer2018-08-22 11:46:55
Android
newdancer, 2018-08-22 11:46:55

How to correctly implement the many to many relationship in Room?

How to correctly implement the many to many relationship in Room?
such tables
5b7d22dc7cc06553596891.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2018-08-22
Kronshtatov @stirr

In short, in the link table, the keys should be described something like this:

@Entity(tableName = "invoiceDetail",
        primaryKeys = { "p_id", "inv_id" },
        foreignKeys = {
                @ForeignKey(entity = Product.class,
                            parentColumns = "p_id",
                            childColumns = "p_id"),
                @ForeignKey(entity = Invoice.class,
                            parentColumns = "inv_id",
                            childColumns = "inv_id")
                })

Well, requests are quite normal - through inner join.
Here is an article on the subject

N
newdancer, 2018-08-22
@newdancer

I tried to get cards with a list of categories in this way, but it does not work. All data is null

public class CardWithCategoryEntity {
    @Embedded
    public CardEntity cardEntity;

    @Ignore
    @Relation(parentColumn = "id", entityColumn = "cards_id", entity = CardCategoryEntity.class, projection = {"cards_id, category_id, record_type"})
    public List<CategoryEntity> categoryList;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question