Declares a junction to be used for joining a relationship.

If a Relation should use an associative table (also known as junction table or join table) then you can use this annotation to reference such table. This is useful for fetching many-to-many relations.

@Entity(primaryKeys = ["pId", "sId"])
data class PlaylistSongXRef(
val pId: Int,
val sId: Int
)

data class PlaylistWithSongs(
@Embedded
val playlist: Playlist,
@Relation(
parentColumns = ["playlistId"],
entity = Song::class,
entityColumns = ["songId"],
associateBy = Junction(
value = PlaylistSongXRef::class,
parentColumns = ["pId"],
entityColumns = ["sId"]
)
)
val songs: List<String>
)

@Dao
interface
MusicDao {
@Query("SELECT * FROM Playlist")
suspend fun getAllPlaylistsWithSongs(): List<PlaylistWithSongs>
}

In the above example the many-to-many relationship between a Song and a Playlist has an associative table defined by the entity PlaylistSongXRef.

See also
Relation

Summary

Public constructors

Junction(
    value: KClass<*>,
    parentColumns: Array<String>,
    entityColumns: Array<String>
)
Cmn

Public properties

Array<String>

The junction columns that will be used to match against the Relation.entityColumns.

Cmn
Array<String>

The junction columns that will be used to match against the Relation.parentColumns.

Cmn
KClass<*>

An entity or database view to be used as a junction table when fetching the relating entities.

Cmn

Public constructors

Junction

Junction(
    value: KClass<*>,
    parentColumns: Array<String> = [],
    entityColumns: Array<String> = []
)

Public properties

entityColumns

val entityColumnsArray<String>

The junction columns that will be used to match against the Relation.entityColumns.

If not specified it defaults to Relation.entityColumns.

parentColumns

val parentColumnsArray<String>

The junction columns that will be used to match against the Relation.parentColumns.

If not specified it defaults to Relation.parentColumns.

value

val valueKClass<*>

An entity or database view to be used as a junction table when fetching the relating entities.

Returns
KClass<*>

The entity or database view to be used as a junction table.