Junction



Declares a junction to be used for joining a relationship.

If a Relation should use an associative table (also know 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"})
public class PlaylistSongXRef {
val pId: Int,
val sId: Int
}
public class PlaylistWithSongs {
@Embedded
val playlist: Playlist
@Relation(
parentColumn = "playlistId",
entity = Song::class,
entityColumn = "songId",
associateBy = Junction(
value = PlaylistSongXRef::class,
parentColumn = "pId",
entityColumn = "sId")
)
val songs: List<String>
}

@Dao
public interface MusicDao {
@Query("SELECT * FROM Playlist")
val 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<*>, parentColumn: String, entityColumn: String)
Cmn

Public properties

String

The junction column that will be used to match against the Relation.entityColumn.

Cmn
String

The junction column that will be used to match against the Relation.parentColumn.

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<*>, parentColumn: String = "", entityColumn: String = "")

Public properties

entityColumn

val entityColumnString

The junction column that will be used to match against the Relation.entityColumn.

If not specified it defaults to Relation.entityColumn.

parentColumn

val parentColumnString

The junction column that will be used to match against the Relation.parentColumn.

If not specified it defaults to Relation.parentColumn.

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.