0

Let's assume i have the following entities:

Entity A:

@Entity(
    tableName = "a_table"
)
data class A(
    val name: String
) {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "a_id")
    var id: Long = 0
}

And Entity B:

@Entity(
    tableName = "b_table"
)
data class B(
    val type: String
) {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "b_id")
    var id: Long = 0
}

Entity B is filled with different types. A Entities can have a reference to B Entities ("M:N" relation). So added the join Entity:

@Entity(
    tableName = "a_b_join",
    primaryKeys = ["fk_a_id", "fk_b_id"],
    foreignKeys = [
        ForeignKey(
            childColumns = arrayOf("fk_a_id"),
            entity = A::class,
            parentColumns = arrayOf("a_id")
        ), ForeignKey(
            childColumns = arrayOf("fk_b_id"),
            entity = B::class,
            parentColumns = arrayOf("b_id")
        )
    ]
)
class ABJoin(
    @ColumnInfo(name = "fk_a_id") var fkAId: Long = 0,
    @ColumnInfo(name = "fk_b_id") var fkBId: Long = 0
)

At the end i want a POJO that looks like this:

class AwithBPojo {
    @Embedded
    lateinit var a : A

    @???
    val listOfB : List<B> = arrayListOf()
}

Why is it so hard for such a simple job? Or am i doing anything wrong? I just want a list of all A objects with the referenced B objects.

I only find tutorials with sample code that looks like this:

class AwithBPojo {
    @Embedded
    lateinit var a : A

    @Relation(
        parentColumn = "b_id",
        projection = ["fk_b_id"],
        entityColumn = "fk_b_id",
        entity = ABJoin::class
    )
    val listOfB : List<Long> = arrayListOf()
}

But i don't want to only have the ID's of entity B because it's a pain to merge the LiveData afterwards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.