# Junction

Artifact: [androidx.room:room-common](https://developer.android.com/jetpack/androidx/releases/room) [View Source](https://cs.android.com/search?q=file:androidx/room/Junction.kt+class:androidx.room.Junction) Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/room#2.2.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/room/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.

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

## Summary

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

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/room/Junction#entityColumn()` The junction column that will be used to match against the `https://developer.android.com/reference/kotlin/androidx/room/Relation#entityColumn()`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/room/Junction#parentColumn()` The junction column that will be used to match against the `https://developer.android.com/reference/kotlin/androidx/room/Relation#parentColumn()`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | `https://developer.android.com/reference/kotlin/androidx/room/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 [2.8.5](https://developer.android.com/jetpack/androidx/releases/room#2.8.5)

```
Junction(value: KClass<*>, parentColumn: String = "", entityColumn: String = "")
```

## Public properties

### entityColumn

Cmn

```
val entityColumn: String
```

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

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

### parentColumn

Cmn

```
val parentColumn: String
```

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

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

### 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. |