Convert-Jpa-To-Spring-Data-Cosmos
Convert-Jpa-To-Spring-Data-Cosmos是一款data方向的AI技能,核心价值是Step-by-step guide for converting Spring Boot JPA applications to use Azure Cosmos DB with Spring Data Cosmos,可用于解决开发者在data领域的实际问题,帮助用户提升效率、自动化重复任务或优化工作流。
Step-by-step guide for converting Spring Boot JPA applications to use Azure Cosmos DB with Spring Data Cosmos
mkdir -p ./skills/convert-jpa-to-spring-data-cosmos && curl -sfL https://raw.githubusercontent.com/github/awesome-copilot/main/skills/convert-jpa-to-spring-data-cosmos/SKILL.md -o ./skills/convert-jpa-to-spring-data-cosmos/SKILL.md Run in terminal / PowerShell. Requires curl (Unix) or PowerShell 5+ (Windows).
Skill Content
# Convert Spring JPA project to Spring Data Cosmos
This generalized guide applies to any JPA to Spring Data Cosmos DB conversion project.
High-level plan
1. Swap build dependencies (remove JPA, add Cosmos + Identity).
2. Add `cosmos` profile and properties.
3. Add Cosmos config with proper Azure identity authentication.
4. Transform entities (ids → `String`, add `@Container` and `@PartitionKey`, remove JPA mappings, adjust relationships).
5. Convert repositories (`JpaRepository` → `CosmosRepository`).
6. **Create service layer** for relationship management and template compatibility.
7. **CRITICAL**: Update ALL test files to work with String IDs and Cosmos repositories.
8. Seed data via `CommandLineRunner`.
9. **CRITICAL**: Test runtime functionality and fix template compatibility issues.
Step-by-step
Step 1 — Build dependencies
- **Maven** (`pom.xml`):
- Remove dependency `spring-boot-starter-data-jpa`
- Remove database-specific dependencies (H2, MySQL, PostgreSQL) unless needed elsewhere
- Add `com.azure:azure-spring-data-cosmos:5.17.0` (or latest compatible version)
- Add `com.azure:azure-identity:1.15.4` (required for DefaultAzureCredential)
- **Gradle**: Apply same dependency changes for Gradle syntax
- Remove testcontainers and JPA-specific test dependencies
Step 2 — Properties and Configuration
- Create `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
```
- Update `src/main/resources/application.properties`:
```properties
spring.profiles.active=cosmos
```
Step 3 — Configuration class with Azure Identity
- Create `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();
}
}
```
- **IMPORTANT**: Use `DefaultAzureCredentialBuilder().build()` instead of key-based authentication for production security
Step 4 — Entity transformation
- Target all classes with JPA annotations (`@Entity`, `@MappedSuperclass`, `@Embeddable`)
- **Base entity changes**:
- Change `id` field type from `Integer` to `String`
- Add `@Id` and `@GeneratedValue` annotations
- Add `@PartitionKey` field (typically `String partitionKey`)
- Remove all `jakarta.persistence` imports
- **CRITICAL - Cosmos DB Serialization Requirements**:
- **Remove ALL `@JsonIgnore` annotations** from fields that need to be persisted to Cosmos DB
- **Authentication entities (User, Authority) MUST be fully serializable** - no `@JsonIgnore` on password, authorities, or other persisted fields
- **Use `@JsonProperty` instead of `@JsonIgnore`** when you need to control JSON field names but still persist the data
- **Common authentication serialization errors**: `Cannot pass null or empty values to constructor` usually means `@JsonIgnore` is blocking required field serialization
- **Entity-specific changes**:
- Replace `@Entity` with `@Container(containerName = "<plural-entity-name>")`
- Remove `@Table`, `@Column`, `@JoinColumn`, etc.
- Remove relationship annotations (`@OneToMany`, `@ManyToOne`, `@ManyToMany`)
- For relationships:
- Embed collections for one-to-many (e.g., `List<Pet> pets` in Owner)
- Use referen
🎯 Best For
- UI designers
- Product designers
- Claude users
- GitHub Copilot users
- Data professionals
💡 Use Cases
- Generating component mockups
- Creating design system tokens
- Data pipeline auditing
- Query optimization
📖 How to Use This Skill
- 1
Install the Skill
Copy the install command from the Terminal tab and run it. The SKILL.md file downloads to your local skills directory.
- 2
Load into Your AI Assistant
Open Claude or GitHub Copilot and reference the skill. Paste the SKILL.md content or use the system prompt tab.
- 3
Apply Convert-Jpa-To-Spring-Data-Cosmos to Your Work
Provide context for your task — paste source material, describe your audience, or share existing work to guide the AI.
- 4
Review and Refine
Edit the AI output for accuracy, tone, and completeness. Add human insight where the AI lacks context.
❓ Frequently Asked Questions
Does this work with Figma?
Some design skills integrate with Figma plugins. Check the Works With section for supported tools.
How do I install Convert-Jpa-To-Spring-Data-Cosmos?
Copy the install command from the Terminal tab and run it. The skill downloads to ./skills/convert-jpa-to-spring-data-cosmos/SKILL.md, ready to use.
Can I customize this skill for my team?
Absolutely. Edit the SKILL.md file to add team-specific instructions, examples, or workflows.
⚠️ Common Mistakes to Avoid
Skipping usability testing
AI-generated designs should be validated with real users before development.
Ignoring data quality
AI analysis inherits all data quality issues — profile your data first.