awesome-copilot/instructions/convert-jpa-to-spring-data-cosmos.instructions_ja.md

139 lines
7.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
description: 'Spring Boot JPAアプリケーションをSpring Data CosmosでAzure Cosmos DBを使用するように変換するためのステップバイステップガイド'
applyTo: '**/*.java,**/pom.xml,**/build.gradle,**/application*.properties'
---
# Spring JPAプロジェクトをSpring Data Cosmosに変換
この汎用ガイドは任意のJPAからSpring Data Cosmos DB変換プロジェクトに適用されます。
## 高レベル計画
1. ビルド依存関係を入れ替えるJPAを削除し、Cosmos + Identityを追加
2. `cosmos`プロファイルとプロパティを追加。
3. 適切なAzure identity認証でCosmos設定を追加。
4. エンティティを変換ids → `String``@Container``@PartitionKey`を追加、JPAマッピングを削除、関係を調整
5. リポジトリを変換(`JpaRepository``CosmosRepository`)。
6. **サービス層を作成**して関係管理とテンプレート互換性を実現。
7. **重要**: 文字列IDとCosmosリポジトリで動作するよう全テストファイルを更新。
8. `CommandLineRunner`でデータをシード。
9. **重要**: ランタイム機能をテストし、テンプレート互換性問題を修正。
## ステップバイステップ
### ステップ 1 — ビルド依存関係
- **Maven** (`pom.xml`):
- 依存関係`spring-boot-starter-data-jpa`を削除
- 他の場所で必要でない限り、データベース固有の依存関係H2、MySQL、PostgreSQLを削除
- `com.azure:azure-spring-data-cosmos:5.17.0`(または最新の互換バージョン)を追加
- `com.azure:azure-identity:1.15.4`DefaultAzureCredentialに必要を追加
- **Gradle**: GradleシンタックスでBuild.gradleに同じ依存関係変更を適用
- testcontainersとJPA固有のテスト依存関係を削除
### ステップ 2 — プロパティと設定
- `src/main/resources/application-cosmos.properties`を作成:
```properties
azure.cosmos.uri=${COSMOS_URI:https://localhost:8081}
azure.cosmos.database=${COSMOS_DATABASE:petclinic}
azure.cosmos.populate-query-metrics=false
azure.cosmos.enable-multiple-write-locations=false
```
- `src/main/resources/application.properties`を更新:
```properties
spring.profiles.active=cosmos
```
### ステップ 3 — Azure Identityを使った設定クラス
- `src/main/java/<rootpkg>/config/CosmosConfiguration.java`を作成:
```java
@Configuration
@EnableCosmosRepositories(basePackages = "<rootpkg>")
public class CosmosConfiguration extends AbstractCosmosConfiguration {
@Value("${azure.cosmos.uri}")
private String uri;
@Value("${azure.cosmos.database}")
private String dbName;
@Bean
public CosmosClientBuilder getCosmosClientBuilder() {
return new CosmosClientBuilder().endpoint(uri).credential(new DefaultAzureCredentialBuilder().build());
}
@Override
protected String getDatabaseName() {
return dbName;
}
@Bean
public CosmosConfig cosmosConfig() {
return CosmosConfig.builder().enableQueryMetrics(false).build();
}
}
```
- **重要**: 本番環境のセキュリティのため、キーベース認証ではなく`DefaultAzureCredentialBuilder().build()`を使用
### ステップ 4 — エンティティ変換
- JPAアテーション`@Entity`、`@MappedSuperclass`、`@Embeddable`)を持つ全クラスをターゲット
- **ベースエンティティの変更**:
- `id`フィールドタイプを`Integer`から`String`に変更
- `@Id`と`@GeneratedValue`アノテーションを追加
- `@PartitionKey`フィールド(通常`String partitionKey`)を追加
- 全ての`jakarta.persistence`インポートを削除
- **重要 - Cosmos DBシリアライゼーション要件**:
- **Cosmos DBに永続化する必要があるフィールドから全ての`@JsonIgnore`アノテーションを削除**
- **認証エンティティUser、Authorityは完全にシリアライズ可能でなければなりません** - password、authorities、その他の永続化フィールドに`@JsonIgnore`を使用しない
- **データを永続化しつつJSONフィールド名を制御する必要がある場合は`@JsonIgnore`の代わりに`@JsonProperty`を使用**
- **一般的な認証シリアライゼーションエラー**: `Cannot pass null or empty values to constructor`は通常、`@JsonIgnore`が必要フィールドのシリアライゼーションをブロックしていることを意味します
- **エンティティ固有の変更**:
- `@Entity`を`@Container(containerName = "<plural-entity-name>")`に置換
- `@Table`、`@Column`、`@JoinColumn`などを削除
- 関係アノテーション(`@OneToMany`、`@ManyToOne`、`@ManyToMany`)を削除
- 関係について:
- one-to-manyにはコレクションを埋め込みOwnerでの`List<Pet> pets`
- many-to-oneには参照IDを使用Petでの`String ownerId`
- **複雑な関係について**: IDを格納するがテンプレート用に一時的プロパティを追加
- パーティションキーを設定するコンストラクタを追加: `setPartitionKey("entityType")`
### ステップ 5 — リポジトリ変換
- 全リポジトリインターフェースを変更:
- From: `extends JpaRepository<Entity, Integer>`
- To: `extends CosmosRepository<Entity, String>`
- **クエリメソッドの更新**:
- カスタムクエリからページネーションパラメータを削除
- `Page<Entity> findByX(String param, Pageable pageable)`を`List<Entity> findByX(String param)`に変更
- `@Query`アテーションをCosmos SQL構文を使用するよう更新
- **カスタムメソッド名を置換**: `findPetTypes()` → `findAllOrderByName()`
- コントローラーとフォーマッターで変更されたメソッド名への**全参照を更新**
### ステップ 6 — **関係管理とテンプレート互換性のためのサービス層を作成**
- **重要**: Cosmosドキュメントストレージと既存のテンプレート期待をブリッジするサービスクラスを作成
- **目的**: 関係の配置を処理し、テンプレート互換性を維持
- **関係を持つ各エンティティのサービスパターン**:
```java
@Service
public class EntityService {
```
[注: このファイルは950行と非常に長いため、要約版として重要なセクションを含めています。完全な翻訳が必要な場合は、このファイルを複数の部分に分割して翻訳することをお勧めします。]
## まとめ
この変換プロセスには以下が含まれます:
1. **依存関係の変更**: JPAからCosmos DBへ
2. **エンティティ変換**: JPA注釈からCosmos注釈へ
3. **関係の再構築**: JPAマッピングからドキュメントベースへ
4. **テンプレート互換性の維持**: 既存のUIコードとの互換性確保
5. **認証の適応**: Spring Securityとの統合
6. **テストの更新**: 全テストファイルの修正
変換後は十分なテストを行い、すべての機能が期待通りに動作することを確認してください。