# Junction

Artifact: [androidx.room3:room3-common](https://developer.android.com/jetpack/androidx/releases/room3) [View Source](https://cs.android.com/search?q=file:androidx/room3/Junction.kt+class:androidx.room3.Junction) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  @Target(allowedTargets = )
  @Retention(value = AnnotationRetention.BINARY)
  annotation Junction
  ```

*** ** * ** ***

Declares a junction to be used for joining a relationship.

If a `https://developer.android.com/reference/kotlin/androidx/room3/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.

```kotlin
@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 |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/Relation` |   |

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/Junction#Junction(kotlin.reflect.KClass,kotlin.Array,kotlin.Array)( value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>, parentColumns: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>, entityColumns: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html> )` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | `https://developer.android.com/reference/kotlin/androidx/room3/Junction#entityColumns()` The junction columns that will be used to match against the `https://developer.android.com/reference/kotlin/androidx/room3/Relation#entityColumns()`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | `https://developer.android.com/reference/kotlin/androidx/room3/Junction#parentColumns()` The junction columns that will be used to match against the `https://developer.android.com/reference/kotlin/androidx/room3/Relation#parentColumns()`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | `https://developer.android.com/reference/kotlin/androidx/room3/Junction#value()` An entity or database view to be used as a junction table when fetching the relating entities. | Cmn |

## Public constructors

### Junction

Cmn Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

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

## Public properties

### entityColumns

Cmn

```
val entityColumns: Array<String>
```

The junction columns that will be used to match against the `https://developer.android.com/reference/kotlin/androidx/room3/Relation#entityColumns()`.

If not specified it defaults to `https://developer.android.com/reference/kotlin/androidx/room3/Relation#entityColumns()`.

### parentColumns

Cmn

```
val parentColumns: Array<String>
```

The junction columns that will be used to match against the `https://developer.android.com/reference/kotlin/androidx/room3/Relation#parentColumns()`.

If not specified it defaults to `https://developer.android.com/reference/kotlin/androidx/room3/Relation#parentColumns()`.

### value

Cmn

```
val value: KClass<*>
```

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | The entity or database view to be used as a junction table. |