Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
giuseppe-trisciuoglio avatar

Spring Data Jpa

  • 1.9k installs
  • 311 repo stars
  • Updated June 22, 2026
  • giuseppe-trisciuoglio/developer-kit

Spring Data JPA skill teaches patterns for creating type-safe repository interfaces, configuring JPA entities with relationships, writing JPQL queries, managing transactions, implementing pagination, and optimizing datab

About

Spring Data JPA provides patterns for building robust persistence layers in Java applications. It enables developers to create repository interfaces with CRUD operations, configure entity relationships with proper cascade types, write derived and custom queries, implement pagination with Pageable, set up database auditing with entity listeners, manage transactions explicitly, and optimize performance through indexing and fetch strategies. This skill covers entity design best practices, N+1 query prevention, pagination validation, and production-safe deployment strategies for complex data models.

  • Repository interfaces extending JpaRepository with derived and @Query methods
  • Entity relationship configuration with cascade types and orphan removal
  • Pagination and sorting using Pageable and PageRequest with large dataset handling
  • Database auditing with @CreatedDate, @LastModifiedDate, @CreatedBy annotations
  • Query optimization through @EntityGraph, indexes, and N+1 prevention

Spring Data Jpa by the numbers

  • 1,880 all-time installs (skills.sh)
  • +67 installs in the week ending Jul 28, 2026 (Skillselion tracking)
  • Ranked #9 of 89 Java & JVM skills by installs in the Skillselion catalog
  • Security screen: MEDIUM risk (skills.sh audit)
  • Data as of Jul 28, 2026 (Skillselion catalog sync)
At a glance

spring-data-jpa capabilities & compatibility

Capabilities
create type safe jparepository interfaces · define jpa entities with proper annotations and · write derived queries using method naming conven · write custom jpql queries with @query annotation · implement pagination with pageable and pagereque · configure database auditing with listener annota · manage transactions with @transactional · optimize queries through @entitygraph and fetch · configure cascade types for entity relationships · add database indexes for query performance
Works with
github · gitlab · postgres · mysql · oracle · sql server · jira
Use cases
api development · database · testing · debugging · refactoring
From the docs

What spring-data-jpa says it does

Never expose JPA entities directly in REST APIs; always use DTOs to prevent lazy loading issues.
SKILL.md#Constraints and Warnings
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill spring-data-jpa

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs1.9k
repo stars311
Security audit3 / 3 scanners passed
Last updatedJune 22, 2026
Repositorygiuseppe-trisciuoglio/developer-kit

What it does

Implement persistence layers with Spring Data JPA repositories, entity relationships, queries, pagination, auditing, and transactions.

Who is it for?

Building Spring Boot microservices, implementing domain-driven design with JPA entities, creating REST APIs backed by relational databases, and establishing team standards for data access patterns.

Skip if: NoSQL database operations, GraphQL query optimization, real-time streaming data, or non-Java technology stacks.

When should I use this skill?

Creating new repository interfaces, configuring entity relationships, writing custom queries, implementing pagination, setting up audit fields, or optimizing query performance.

What you get

Developer can build maintainable persistence layers with Spring Data JPA repositories, properly configured entities, optimized queries, pagination for large datasets, and database auditing.

  • JpaRepository interface implementations
  • Configured JPA entities with relationships
  • JPQL query methods with @Query

By the numbers

  • Supports CRUD operations on any entity type through JpaRepository inheritance
  • Handles pagination through Pageable interface with configurable page sizes
  • Prevents N+1 queries through @EntityGraph annotations

Files

SKILL.mdMarkdownGitHub ↗

Spring Data JPA

Overview

Provides patterns for Spring Data JPA repositories, entity relationships, queries, pagination, auditing, and transactions.

When to Use

Creating repositories with CRUD operations, entity relationships, @Query annotations, pagination, auditing, or UUID primary keys.

Instructions

Create Repository Interfaces

To implement a repository interface:

1. Extend the appropriate repository interface:

   @Repository
   public interface UserRepository extends JpaRepository<User, Long> {
       // Custom methods defined here
   }

2. Use derived queries for simple conditions:

   Optional<User> findByEmail(String email);
   List<User> findByStatusOrderByCreatedDateDesc(String status);

3. Implement custom queries with `@`Query:

   @Query("SELECT u FROM User u WHERE u.status = :status")
   List<User> findActiveUsers(@Param("status") String status);

Configure Entities

1. Define entities with proper annotations:

   @Entity
   @Table(name = "users")
   public class User {
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       private Long id;

       @Column(nullable = false, length = 100)
       private String email;
   }

2. Configure relationships using appropriate cascade types:

   @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
   private List<Order> orders = new ArrayList<>();

Validation: Test cascade behavior with a small dataset before applying to production data. Verify delete operations don't cascade unexpectedly.

3. Set up database auditing:

   @CreatedDate
   @Column(nullable = false, updatable = false)
   private LocalDateTime createdDate;

Apply Query Patterns

1. Use derived queries for simple conditions 2. Use `@`Query for complex queries 3. Return Optional<T> for single results 4. Use Pageable for pagination 5. Apply `@`Modifying for update/delete operations

Manage Transactions

1. Mark read-only operations with `@`Transactional(readOnly = true) 2. Use explicit transaction boundaries for modifying operations 3. Specify rollback conditions when needed

Validate and Optimize

1. Verify entity configuration:

  • Test cascade behavior in a transaction before production deployment
  • Validate bidirectional relationships sync correctly

2. Optimize query performance:

  • Run EXPLAIN ANALYZE on queries against large tables
  • If performance issues detected: add indexes → verify with EXPLAIN → repeat
  • Use @EntityGraph to prevent N+1 queries

3. Validate pagination:

  • Ensure indexed columns support pagination queries
  • Test with large datasets to verify cursor stability

Examples

Basic CRUD Repository

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
    // Derived query
    List<Product> findByCategory(String category);

    // Custom query
    @Query("SELECT p FROM Product p WHERE p.price > :minPrice")
    List<Product> findExpensiveProducts(@Param("minPrice") BigDecimal minPrice);
}

Pagination Implementation

@Service
public class ProductService {
    private final ProductRepository repository;

    public Page<Product> getProducts(int page, int size) {
        Pageable pageable = PageRequest.of(page, size, Sort.by("name").ascending());
        return repository.findAll(pageable);
    }
}

Entity with Auditing

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @CreatedDate
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdDate;

    @LastModifiedDate
    private LocalDateTime lastModifiedDate;

    @CreatedBy
    @Column(nullable = false, updatable = false)
    private String createdBy;
}

Best Practices

Entity Design

  • Use constructor injection exclusively (never field injection)
  • Prefer immutable fields with final modifiers
  • Use Java records (16+) or @Value for DTOs
  • Always provide proper @Id and @GeneratedValue annotations
  • Use explicit @Table and @Column annotations

Performance Optimization

  • Use appropriate fetch strategies (LAZY vs EAGER)
  • Implement pagination for large datasets
  • Use database indexes for frequently queried fields
  • Consider using @EntityGraph to avoid N+1 query problems

Reference Documentation

For comprehensive examples, detailed patterns, and advanced configurations, see:

  • Examples - Complete code examples for common scenarios
  • Reference - Detailed patterns and advanced configurations

Constraints and Warnings

  • Never expose JPA entities directly in REST APIs; always use DTOs to prevent lazy loading issues.
  • Avoid N+1 query problems by using @EntityGraph or JOIN FETCH in queries.
  • Be cautious with CascadeType.REMOVE on large collections as it can cause performance issues.
  • Do not use EAGER fetch type for collections; it can cause excessive database queries.
  • Avoid long-running transactions as they can cause database lock contention.
  • Use @Transactional(readOnly = true) for read operations to enable optimizations.
  • Be aware of the first-level cache; entities may not reflect database changes within the same transaction.
  • UUID primary keys can cause index fragmentation; consider using sequential UUIDs or Long IDs.
  • Pagination on large datasets requires proper indexing to avoid full table scans.

Related skills

Forks & variants (1)

Spring Data Jpa has 1 known copy in the catalog totaling 21 installs. They canonicalize to this original listing.

How it compares

Pick spring-data-jpa for annotated Spring entity/repository scaffolding instead of generic Java POJO templates without persistence mappings.

FAQ

When should I use @Query vs derived query methods?

Use derived queries for simple single-condition lookups (findByEmail). Use @Query for complex multi-condition queries, joins, or when readability requires explicit JPQL. @Query provides better performance and maintainability for complex logic.

How do I prevent N+1 query problems?

Use @EntityGraph to eagerly load related entities, apply JOIN FETCH in @Query methods, or adjust fetch strategies from EAGER to LAZY. Always verify with EXPLAIN ANALYZE before production deployment to confirm query counts.

What cascade type should I use for entity relationships?

Use CascadeType.ALL with orphanRemoval=true for child entities owned exclusively by parent. Use CascadeType.PERSIST for optional relationships. Always test cascade behavior with small datasets before production to prevent unexpected deletes.

Is Spring Data Jpa safe to install?

skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

Java & JVMbackendtesting

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.