---
title: Use Spring Data read models
editUrl: https://github.com/Cratis/Arc.Kotlin/edit/main/Documentation/guides/spring-data/index.mdx
description: Add Arc Spring Data integrations for Kotlin and Java to inject JPA or MongoDB repositories into model-bound commands and queries with generated adapters.
lastUpdated: 2026-11-05T00:00:00.000Z
---

## Add an integration

Start with the store your application already uses. Choose the JPA integration for a relational store or the MongoDB integration for MongoDB; you do not need both to use a repository:

```kotlin
// JPA
dependencies {
    implementation("io.cratis:arc-spring-data-jpa:<version>")
}
```

```kotlin
// MongoDB
dependencies {
    implementation("io.cratis:arc-spring-data-mongodb:<version>")
}
```

Each module includes the Arc Spring Boot starter and the corresponding Spring Data starter. Configure the chosen store through Spring Boot as usual: it still owns the datasource, entity manager, Mongo client, repository discovery, and their standard configuration.

## Inject a repository into a query

Once Spring has discovered the application's repository, use it as an ordinary Spring service. In a [model-bound query](/arc/backend/kotlin/guides/queries/#add-a-query-to-a-read-model), mark the repository parameter with `@FromServices`. Arc's generated performer then obtains that dependency from the current application context rather than treating it as caller input.

**Java**

```java
@Entity
@ReadModel
public class TaskView {
    @Id
    public String id;
    public String title;

    public static List<TaskView> all(@FromServices TaskViewRepository tasks) {
        return tasks.findAll();
    }
}

interface TaskViewRepository extends JpaRepository<TaskView, String> { }
```

[View Java snippet source on GitHub](https://github.com/Cratis/Arc.Kotlin/blob/main/Documentation/client-snippets-java/guides/spring-data/index/repository-in-query.md)

**Kotlin**

```kotlin
@Entity
@ReadModel
data class TaskView(@Id val id: String = "", val title: String = "") {
    companion object {
        @JvmStatic
        fun all(@FromServices tasks: TaskViewRepository): List<TaskView> = tasks.findAll()
    }
}

interface TaskViewRepository : JpaRepository<TaskView, String>
```

[View Kotlin snippet source on GitHub](https://github.com/Cratis/Arc.Kotlin/blob/main/Documentation/client-snippets/guides/spring-data/index/repository-in-query.md)

In the example, the query calls the injected repository and returns its results. The Kotlin companion method and Java static method expose the same read operation; neither creates the repository or configures its store.

The same injection pattern works with `MongoRepository`, repository fragments, and application query services. An application bean replaces an auto-configured adapter bean of the same integration contract. Repository injection alone does not select a tenant's storage or subscribe to changes.

## Then

Continue with the operation you need. Concept mapping is an optional branch for the chosen store, not a requirement to configure both integrations:

- [Repositories in queries and commands](/arc/backend/kotlin/guides/spring-data/repositories/) — consume paging, resolve a command's current state, choose tenant-bound access, and opt into supported transactions.
- [Map JPA concepts](/arc/backend/kotlin/guides/spring-data/concepts-jpa/) — use attribute converters and embedded ids when a JPA model stores domain concepts.
- [Map MongoDB concepts](/arc/backend/kotlin/guides/spring-data/concepts-mongodb/) — register concrete scalar pairs and identifier mappings when a MongoDB document stores domain concepts.
- [Observable storage snapshots](/arc/backend/kotlin/guides/spring-data/observable-snapshots/) — return replacement query results as storage changes, using MongoDB change streams or explicit JPA notifications.
