Spring Data JPA Articles
-
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 Specifications - Handling Search Form Filtering with JpaSpecificationExecutor
An implementation guide for Spring Data JPA Specifications 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 Specifications and QueryDSL, all explained 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.
-
Have You Ever Been Unsure How to Write Spring Data JPA Query Methods?
A reference table summarizing the naming conventions of Spring Data JPA query methods (findBy/existsBy/countBy/deleteBy). Check keywords like Containing/StartingWith/GreaterThan/Between/In/IsNull along with their generated SQL and usage examples in a quick-reference table. Also explains custom queries with @Query, sorting, and paging with practical examples.