Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

spring-data-jpa

/spring-data-jpa

Dodaje trwałość JPA do istniejącego projektu Spring Boot 4 z encjami, repozytoriami, migracjami Flyway i testami.

Licencja
MIT
Ostatnia zmiana repozytorium
11.11.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
2
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
1
Przestarzałość

Brak sygnałów. Repozytorium zmienione 11.11.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Dodaje trwałość JPA do istniejącego projektu Spring Boot 4 z encjami, repozytoriami, migracjami Flyway i testami.

Oryginalny opis (EN)

Add JPA persistence to an existing Spring Boot 4 Maven project — entities, repositories, auditing, Flyway migrations, and Testcontainers integration tests. Use when asked to add a database, JPA entities or repositories, or Flyway migrations. Assumes a feature-sliced layout like spring-scaffold generates.

Jak działa

  1. Step 0 — Gather inputs
  2. Step 1 — Read the project
  3. Step 2 — Add dependencies
  4. Step 3 — Add JPA config
  5. Step 4 — Create entities
  6. Step 5 — Create repositories
  7. Step 6 — Write Flyway migrations
  8. Step 7 — Update application.yml
  9. Step 8 — Write tests
  10. Step 9 — Run and report
Sekcje z nagłówków SKILL.md, w oryginale · 327 linii, 1 005 słów
Podgląd SKILL.md
---
name: spring-data-jpa
description: "Add JPA persistence to an existing Spring Boot 4 Maven project — entities, repositories, auditing, Flyway migrations, and Testcontainers integration tests. Use when asked to add a database, JPA entities or repositories, or Flyway migrations. Assumes a feature-sliced layout like spring-scaffold generates."
---

# Spring Data JPA Skill

Adds a production-ready persistence layer to an existing Spring Boot 4 project.

`SKILL_DIR` = directory containing this SKILL.md file.

Load `SKILL_DIR/references/jpa-conventions.md` before writing entities or migrations — it holds the
house rules on IDs, auditing, repository choice, migration discipline, and when to reach for
`JpaRepository`.

**Comments:** minimal, and only `WHY`. Generated code should be obvious from names and types. One short
line max — never a multi-line comment block.

---

## Step 0 — Gather inputs

| Field | Required | Notes |
|-------|----------|-------|
| `entities` | Yes | list of domain entities to add (e.g. `Order`, `OrderLine`) |
| `groupId` | No | project groupId; read from `pom.xml` if missing |
| `package` | No | base package; read from `pom.xml` if missing |
| `idType` | No | `Long` (default) or `UUID` |
| `auditing` | No | `true` (default) — add `@CreatedDate` / `@LastModifiedDate` |

---

## Step 1 — Read the project

```bash
cat pom.xml
ls src/main/java/<package>/
ls src/test/java/<package>/
cat src/main/resources/application.yml 2>/dev/null || cat src/main/resources/application.properties 2>/dev/null
```

Confirm:
- Spring Boot 4.x is in use.
- A `BaseIntegrationTest` exists in `src/test/java`.
- The project uses `application.yml` or `application.properties`.

If Boot is not 4.x, stop and say so — the property keys and Testcontainers shapes differ.

---

## Step 2 — Add dependencies

Add these to `pom.xml` if missing:

```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-database-postgresql</artifactId>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>
```

Do **not** add `org.flywaydb:flyway-core` directly. The Boot starter owns it transitively and brings
the matching auto-configuration. `flyway-database-postgresql` is the Flyway 10+ module that knows the
Postgres dialect; without it Flyway silently fails to find migrations on Postgres.

If `spring-boot-starter-validation` is missing, add it — request DTOs need it.

---

## Step 3 — Add JPA config

Create `src/main/java/<package>/shared/config/JpaConfig.java`:

```java
package <package>.shared.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class JpaConfig {
}
```

If the project already has a main class annotated with `@EnableJpaAuditing`, skip this file and say
why.

---

## Step 4 — Create entities

For each entity in `entities`, create `src/main/java/<package>/<entity>/<Entity>.java`.

Example for `Job` with `Long` IDs and auditing:

```java
package <package>.job;

import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.Instant;

@Entity
@Table(name = "job")
@EntityListeners(AuditingEntityListener.class)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Job {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String title;

    private String description;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 20)
    private JobStatus status;

    @CreatedDate
    @Column(nullable = false, updatable = false)
    private Instant createdAt;

    @LastModifiedDate
    @Column(nullable = false)
    private Instant updatedAt;
}
```

Rules from `references/jpa-conventions.md`:

- `Instant` for timestamps, never `java.util.Date`.
- `@Enumerated(EnumType.STRING)` for enums — never the default ordinal.
- Table names explicit with `@Table`, singular.
- `@Column(nullable = false)` matches the Flyway migration; no drift between code and schema.
- Lombok `@Getter`/`@Setter` on entities is allowed; no Lombok in controller/service layer.

If `idType=UUID`:

```java
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
```

Boot 4.1 maps `GenerationType.UUID` to a UUID generator without a Hibernate extension.

---

## Step 5 — Create repositories

For each entity, create `src/main/java/<package>/<entity>/<Entity>Repository.java`:

```java
package <package>.job;

import org.springframework.data.jpa.repository.JpaRepository;

public interface JobRepository extends JpaRepository<Job, Long> {
}
```

Use `JpaRepository` only when pagination or scrolling is needed. Otherwise prefer
`ListCrudRepository<Entity, ID>` — see `references/jpa-conventions.md` for the decision table.

---

## Step 6 — Write Flyway migrations

Write `src/main/resources/db/migration/V1__init.sql` with `CREATE TABLE` for every entity. Example:

```sql
CREATE TABLE job (
    id BIGSERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    status VARCHAR(20) NOT NULL,
    created_at TIMESTAMPTZ NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX idx_job_status_created_at ON job (status, created_at);
```

Guidelines:
- Types: `BIGSERIAL` for `Long`, `UUID` for `UUID`, `TIMESTAMPTZ` for `Instant`, `VARCHAR(n)` with a
  real bound, `TEXT` for unbounded strings.
- Every migration is `V<N>__description.sql`, two underscores, no spaces.
- `ddl-auto: validate` in `application.yml` means the app refuses to start if entities and schema
  drift. Migrations are the source of truth.
- Index foreign keys and query columns; leave audit columns indexed only when queried.

If auditing is enabled, include `created_at`/`updated_at` columns in `V1__init.sql`. Do not split
auditing into a separate migration unless retrofitting an existing schema.

---

## Step 7 — Update application.yml

Add or merge:

```yaml
spring:
  datasource:
    url: jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/${DB_NAME:appdb}
    username: ${DB_USER:app}
    password: ${DB_PASSWORD:secret}
  jpa:
    hibernate:
      ddl-auto: validate
    open-in-view: false
    properties:
      hibernate:
        jdbc:
          time_zone: UTC
  flyway:
    enabled: true
    baseline-on-migrate: true
```

`ddl-auto: validate` is non-negotiable. `create`/`create-drop`/`update` are banned in every
environment except a throwaway local experiment — and even then Flyway owns the schema.

`open-in-view: false` prevents lazy-loading outside transactions; map to DTOs in the service layer.

---

## Step 8 — Write tests

If a `BaseIntegrationTest` exists, create repository integration tests next to it:

```java
package <package>.job;

import <package>.BaseIntegrationTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;

import static org.assertj.core.api.Assertions.assertThat;

class JobRepositoryIntegrationTest extends BaseIntegrationTest {

    @Autowired
    private JobRepository jobRepository;

    @Test
    @Sql("/sql/job-test-data.sql")
    void should_find_open_jobs() {
        var jobs = jobRepository.findAllByStatus(JobStatus.OPEN);

        assertThat(jobs).hasSize(2).allMatch(j -> j.getStatus() == JobStatus.OPEN);
    }
}
```

If no `BaseIntegrationTest` exists, create it and its container configuration first, using the
pattern from `spring-scaffold`:

```java
@TestConfiguration(proxyBeanMethods = false)
public class IntegrationTestContainers {

    @Bean
    @ServiceConnection
    PostgreSQLContainer postgresContainer() {
        return new PostgreSQLContainer(DockerImageName.parse("postgres:18-alpine"));
    }
}
```

```java
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import(IntegrationTestContainers.class)
public abstract class BaseIntegrationTest {
}
```

The container is a Spring bean, not a `static @Container` field: the JUnit extension would stop it
after the first test class while Spring keeps the cached context, and the next class would fail with
`Connection refused`.

Before writing, confirm `18-alpine` is still current:

```bash
curl -s "https://hub.docker.com/v2/repositories/library/postgres/tags/18-alpine" \
  | python3 -c "import json,sys; d=json.load(sys.stdin); print(d['name'], d['last_updated'][:10])"
```

Also write `src/test/resources/sql/job-test-data.sql` with `INSERT` statements matching the migration.

---

## Step 9 — Run and report

```bash
./mvnw test
```

Report:

- Which dependencies were added
- Entities and repositories created
- Migration file path
- Whether `BaseIntegrationTest` was created or reused
- Test result summary
- Next step: add service/controller layers or run the `api-design` skill for OpenAPI contracts

If the app is meant to run locally, point the user at `devops-scaffold` to add a `docker-compose.yml`
service for Postgres.
Pliki w paczce (2)
  • SKILL.md 9 KB
  • references/jpa-conventions.md 4 KB

Wersje

RepozytoriumWynikLicencja
Ta strona · swap-cameroon/kuro-antigravity-config100MIT
aratkruglik/claude-sdlc100MIT
rrezartprebreza/spring-boot-skills100MIT
claude-dev-suite/claude-dev-suite100MIT
rrezartprebreza/spring-boot-skills100MIT
giuseppe-trisciuoglio/developer-kit100MIT

Migawka bazy z 13.11.2026 · wersja paczki 5eb3fe84a511 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100spring-boot
    Pomaga tworzyć usługi Spring Boot 4.x: kontrolery REST, encje JPA z Flyway, zabezpieczenia OAuth2 i testy Testcontainers.
  • 100spring-boot-data-access
    Optymalizuje JPA, Hibernate i dostęp do bazy w Spring Boot, przy implementacji encji i repozytoriów.
  • 100313-frameworks-spring-db-migrations-flyway
    Dodaje i sprawdza migracje bazy danych Flyway w Spring Boot: zależności, skrypty db/migration, konfigurację i walidację.
  • 100spring-testing-persistence
    Testuje warstwę trwałości Spring Boot: repozytoria, zapytania, mapowania JPA, ograniczenia bazy i migracje z Testcontainers.
  • 100spring-boot
    Wspiera budowę aplikacji w Spring Boot 4 (Spring Framework 7): REST API z JPA i Flyway, zabezpieczenia JWT, Actuator i testy MockMvc.
  • 95spring-boot
    Podpowiada wzorce Spring Boot 3: kontrolery REST, serwisy, repozytoria, encje JPA, mapery MapStruct, Lombok, zabezpieczenia JWT, migracje Flyway i globalną obsługę wyjątków.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.