Spring Data JPA Articles
11 articles about Spring Data JPA. Explore Spring Boot implementation, design, and operations across related topics.
-
How to Use the @Query Annotation in Spring Boot + Spring Data JPA - From JPQL and Native SQL to Update Queries with @Modifying
A hands-on guide to mastering the @Query annotation in Spring Boot + Spring Data JPA. Covers the basics of JPQL and @Param, when to use nativeQuery=true, UPDATE/DELETE with @Modifying + @Transactional, countQuery when combined with Pageable, SpEL (:#{}), and escaping LIKE searches, all explained with copy-and-paste-ready repository code.
-
How to Speed Up Bulk INSERTs with Spring Data JPA - hibernate.jdbc.batch_size and Batching saveAll
Explains why calling saveAll() still results in INSERTs being executed one at a time, covering the mechanics of hibernate.jdbc.batch_size, order_inserts, and IDENTITY generation. From PostgreSQL's reWriteBatchedInserts to a comparison with JdbcTemplate.batchUpdate, it walks through the steps to speed up bulk INSERTs while verifying with SQL logs.
-
How to Persist enums to a Database with Spring Boot + JPA - Choosing Between @Enumerated(EnumType.STRING) and AttributeConverter
A guide to persisting enum fields to database columns with Spring Boot + JPA. Covers the difference between @Enumerated(STRING/ORDINAL), the dangers of ORDINAL, implementing an AttributeConverter for custom code values, and criteria for choosing between the two approaches, with practical examples.
-
How to Fix LazyInitializationException in Spring Boot + JPA - Causes and Solutions for "could not initialize proxy - no Session"
Solve the LazyInitializationException (could not initialize proxy - no Session) that crashes the moment you access a lazily-loaded association in Spring Data JPA, working backwards from how Hibernate sessions and transaction boundaries operate. Compares the four proper approaches—JOIN FETCH, @EntityGraph, DTO projection, and @Transactional—and provides criteria for making decisions without resorting to open-in-view=true or switching to EAGER.
-
How to Retrieve DTOs Directly with Spring Data JPA Projections - Choosing Between Interface-Based and Class-Based Projections
Learn how to SELECT only the columns you need and project them directly into DTOs/interfaces with Spring Data JPA projections. Compares interface-based, class-based (constructor), and dynamic projections through implementations, covering differences in the generated SQL, selection criteria, and pitfalls when combined with @Query.
-
How to Work with Multiple DataSources in Spring Boot - Read/Write Separation and AbstractRoutingDataSource
A guide to implementing multiple DataSources in Spring Boot, covering two patterns: a static multi-DataSource configuration and dynamic read/write separation using AbstractRoutingDataSource. It walks through how to route to the primary/replica triggered by @Transactional(readOnly=true), organized with copy-and-run code.
-
How to Implement Optimistic Locking (@Version) with JPA in Spring Boot to Prevent Concurrent Update Conflicts
Learn how to prevent 'Lost Update' issues that occur in e-commerce inventory updates and reservation systems using the @Version annotation. Covers OptimisticLockException handling, retry strategies with Spring Retry, and writing concurrency tests with practical code examples.
-
How to Implement Dynamic Queries with Spring Data JPA Specification - Supporting Search Form Filtering with JpaSpecificationExecutor
A hands-on guide to Spring Data JPA Specification that eliminates if-statement hell in search forms. Covers setting up JpaSpecificationExecutor, safely skipping null conditions, combining multiple conditions with AND/OR, integrating with pagination, and choosing between Specification and QueryDSL, all with practical code examples.
-
How to Solve the N+1 Problem in Spring Data JPA - When to Use @EntityGraph vs JOIN FETCH
A practical guide covering how to detect the N+1 problem in Spring Data JPA and how to resolve it using @EntityGraph and JOIN FETCH with real examples. Covers Lazy/Eager Loading configuration and batch fetch optimization techniques useful in production environments.
-
How to Automatically Record Created and Updated Timestamps with JPA Auditing in Spring Boot
A guide to automatically recording entity creation and update timestamps using JPA Auditing in Spring Boot. Covers practical code for configuring @CreatedDate, @LastModifiedDate, @EnableJpaAuditing, and AuditorAware, including integration with Spring Security.
-
Spring Data JPA Query Methods: A Complete Naming Convention Cheat Sheet
A cheat sheet organizing Spring Data JPA query method naming conventions (findBy/existsBy/countBy/deleteBy) in reference tables. Check keywords like Containing/StartingWith/GreaterThan/Between/In/IsNull alongside the generated SQL and usage examples. Also covers custom queries with @Query, sorting, and paging with practical examples.