JPA Articles
Articles covering JPA relationship mapping, query methods, transactions, and performance optimization for data access design.
-
How to Automatically Record Change History (Audit Logs) with Spring Boot + Hibernate Envers
A guide to implementing automatic recording of the full revision history of "who changed what, when, and how" in Spring Boot using Hibernate Envers. Covers _AUD table generation with @Audited, retrieving past versions with AuditReader, attaching user information to revisions, and strategies to prevent history table bloat.
-
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 Implement Pessimistic Locking (@Lock) with Spring Boot + JPA - PESSIMISTIC_WRITE and Deadlock Prevention
Learn how to apply PESSIMISTIC_WRITE/READ using Spring Data's JPA @Lock, verify the issuance of SELECT ... FOR UPDATE, control timeouts with jakarta.persistence.lock.timeout, avoid deadlocks, and choose between pessimistic and optimistic locking—explained with real code.
-
How to Load Initial Data with data.sql and schema.sql in Spring Boot - When to Use Flyway/Liquibase Instead
A guide to SQL initialization with schema.sql/data.sql in Spring Boot. It covers why data.sql doesn't get executed, spring.sql.init.mode, the execution order relative to Hibernate ddl-auto, configuring defer-datasource-initialization, and criteria for choosing Flyway/Liquibase in production.
-
How to Write Repository Layer Slice Tests with @DataJpaTest in Spring Boot - Choosing Between H2 and Testcontainers
A guide to writing slice tests focused on the Repository layer using @DataJpaTest in Spring Boot 3.x. Covers automatic rollback, TestEntityManager, switching between H2 and a real database via @AutoConfigureTestDatabase, verifying query methods and @Query, and when to combine Testcontainers — all with practical examples.
-
How to Reduce Boilerplate Code with Lombok in Spring Boot
A practical explanation of the roles and proper usage of Lombok annotations frequently used in Spring Boot development (@Data, @Builder, @RequiredArgsConstructor, @Slf4j, etc.). Also covers pitfalls when used with JPA Entity and how to combine it with constructor injection.
-
How to Implement Soft Delete with Spring Boot + JPA - Choosing Between @SQLDelete, @SQLRestriction, and Filter
This article explains how to transparently implement soft delete (logical deletion) with Spring Boot 3.x + Hibernate 6.4. It organizes a comparison between @SQLDelete + @SQLRestriction (formerly @Where) and @SoftDelete / @FilterDef so you can quickly choose the right approach, and summarizes common pitfalls such as unique constraint conflicts, restoration handling, and recording who deleted the record.
-
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 Type-Safe Dynamic Queries with QueryDSL in Spring Boot
A practical guide to integrating QueryDSL into Spring Boot, covering Q-type class generation via APT, dynamic query implementation with JPAQueryFactory, and pagination integration. Includes a comparison with Specification.
-
How to Automate Entity-DTO Mapping with MapStruct in Spring Boot
An implementation guide for auto-generating toDto()/toEntity() methods with MapStruct instead of writing them by hand. Covers adding dependencies, basic @Mapper usage, nested objects, custom conversions, and unit testing.
-
Implementing REST API CRUD with Spring Boot - Basic Structure of Controller, Service, and Repository
A step-by-step guide to implementing REST API CRUD (Create, Read, Update, Delete) with Spring Boot using a three-layer structure of Controller, Service, and Repository. Get the four GET/POST/PUT/DELETE endpoints running via copy-paste, with end-to-end coverage including verification using curl.
-
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.
-
MyBatis vs JPA in Spring Boot - Selection Criteria and Combined Usage Patterns [With Comparison Table]
Wondering whether to choose MyBatis or JPA for your Spring Boot project? This article organizes SQL control flexibility, learning cost, performance, and maintainability in a comparison table, and explains practical decision flowcharts, the difference between #{} and ${}, and concrete configuration examples for using both together. Get all the decision criteria in one place.
-
Understanding Transaction Management with @Transactional in Spring Boot - How to Use Propagation Levels and Isolation Levels
A comprehensive guide to transaction management using the @Transactional annotation in Spring Boot, from basics to practical usage. Covers default behavior, all 7 propagation levels, 4 isolation levels, and common failure patterns where rollback does not work (checked exceptions, self-invocation) with real examples.
-
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.
-
Introduction to Spring Boot JPA Relationship Mapping - How to Use @OneToMany/@ManyToOne/@ManyToMany and mappedBy
A beginner-friendly explanation of @OneToMany/@ManyToOne/@ManyToMany/mappedBy in Spring Boot JPA. Resolves common pitfalls with code examples covering the differences between bidirectional/unidirectional relationships, cascade, FetchType, the N+1 problem, and circular reference countermeasures.