Junction
@Target(allowedTargets = )
@Retention(value = AnnotationRetention.BINARY)
public annotation Junction
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 |
|---|
Public methods |
|
|---|---|
final @NonNull String[] |
The junction columns that will be used to match against the |
final @NonNull String[] |
The junction columns that will be used to match against the |
final @NonNull KClass<@NonNull ?> |
getValue()An entity or database view to be used as a junction table when fetching the relating entities. |
Public constructors
Public methods
getEntityColumns
public final @NonNull String[] getEntityColumns()
The junction columns that will be used to match against the Relation.entityColumns.
If not specified it defaults to Relation.entityColumns.
getParentColumns
public final @NonNull String[] getParentColumns()
The junction columns that will be used to match against the Relation.parentColumns.
If not specified it defaults to Relation.parentColumns.