Spring Data JDBC vs JPA vs Raw JDBC
Fast recall
Quick revision
Core answer
Awareness of the broader Spring data access ecosystem beyond just JPA and raw JDBC
Confusing Spring Data JDBC with JdbcTemplate or Spring JDBC — they are different tools with different purposes
Explain Spring Data JDBC's aggregate root model and position it as a middle ground between JPA's magic and JdbcTemplate's verbosity
Interview-ready guide
Interview answer
Use Spring Data JDBC when aggregate boundaries are clear and the application wants repository support without a session-level unit of work. Use JPA when managed relationships, JPQL, or entity lifecycle behavior fits the domain. Use JdbcTemplate for a report or bulk operation naturally expressed as SQL. They can coexist in separate repositories, but transaction-manager configuration must be coherent.
For example, I may use raw JDBC for a complex reporting SELECT, Spring Data JDBC for a simple order aggregate, and JPA when entity relationships and change tracking reduce application code. The choice follows the model and queries, not a blanket speed ranking.
Spring Data JDBC vs JPA vs Raw JDBC
Direct Answer
Spring Data JDBC is an aggregate-oriented mapping and repository framework without JPA's persistence context, lazy loading, dirty checking, or general bidirectional relationship model. It generates SQL for repository operations and can run declared/custom queries. Direct JDBC or JdbcTemplate keeps SQL and row mapping explicit. JPA manages entity state and richer associations.
Code Example
// Java 17 + Spring Data JDBC: repository like JPA, but predictable
import org.springframework.data.annotation.Id;
import org.springframework.data.repository.ListCrudRepository;
class Order { // plain class, Spring Data JDBC annotations
@Id private Long id;
private String status;
// getters / setters
}
interface OrderRepository extends ListCrudRepository<Order, Long> { }
// Usage:
Order order = orderRepository.findById(1L).orElseThrow(); // one SELECT, eager
order.setStatus("SHIPPED");
// Nothing persists yet — there is NO dirty checking:
orderRepository.save(order); // you must call save() to fire the UPDATE
Unlike JPA, modifying the entity does nothing until you explicitly call save() — no hidden flush.
Common Mistakes & Pitfalls
- Confusing it with JdbcTemplate: Freshers think Spring Data JDBC is just a renamed version of JdbcTemplate, when they're completely different tools. JdbcTemplate requires you to write SQL and map results; Spring Data JDBC generates SQL from repository methods and entity annotations.
- Expecting JPA features: Beginners use Spring Data JDBC expecting lazy loading or automatic dirty checking, then are surprised when every load is eager and every save requires explicit calls. They don't realize it's intentionally simpler than JPA.
- Assuming it's always lighter weight: Some freshers think Spring Data JDBC is automatically faster than JPA, but its eager loading can fetch more data than necessary. For read-heavy apps with complex graphs, JPA's lazy loading and caching might actually perform better.
Check your understanding
Answer these without looking back at the article.
- What is an aggregate root in Spring Data JDBC?
- Why does Spring Data JDBC not support lazy loading?
- How do you write custom queries in Spring Data JDBC?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.