When to Choose JDBC over JPA
Fast recall
Quick revision
Core answer
Practical decision-making and understanding that JPA and JDBC are complementary tools, not either-or choices
Thinking you must choose one exclusively — real Spring Boot apps often use both JPA and JdbcTemplate together
Mention that Spring Data JPA's @Query with native SQL and JdbcTemplate provide SQL control while keeping JPA for CRUD
Interview-ready guide
Interview answer
In a Spring Boot project, I'd choose JDBC over JPA for the same reasons as any Java app, but Spring makes mixing both really easy.
The key insight is that you don't have to pick one exclusively. Most of my application would use JPA repositories — creating users, updating order statuses, managing relationships between entities. That's where JPA shines.
But for specific scenarios, I'd inject JdbcTemplate and write raw SQL.
For example, if I need a monthly sales report that joins five tables with grouping and aggregations, that's awkward in JPQL. I'd write it as plain SQL with JdbcTemplate. Same for bulk operations.
Let's say I need to archive orders older than two years. Loading fifty thousand entities into memory and deleting them through JPA is inefficient. With JdbcTemplate I just execute DELETE FROM orders WHERE created_at is less than two years ago.
One database call, done. Spring Boot makes this practical because you can inject both tools in the same service class. I might have OrderRepository for normal CRUD and JdbcTemplate for that archival job or reporting queries.
They work together fine. One thing to mention — Spring Data JPA does support native SQL with the @Query annotation and nativeQuery equals true. That's a middle ground.
But if your query is really complex and the result doesn't map to an entity, writing a custom RowMapper with JdbcTemplate is often cleaner than configuring @SqlResultSetMapping. The takeaway is use JPA by default for entity operations but don't be afraid to reach for JdbcTemplate when the query doesn't fit the entity model or performance matters.
When to Choose JDBC over JPA
Direct Answer
Choose JDBC (or JdbcTemplate) over JPA in Spring Boot when you need complex SQL that doesn't map well to entities — bulk updates, intricate joins with aggregations, database-specific features, or performance-critical queries where ORM overhead matters. JPA and JdbcTemplate happily coexist in the same application.
Code Example
// Java 17 + Spring Boot: JPA for CRUD, JdbcTemplate for a report
@Service
class OrderService {
private final OrderRepository orders; // Spring Data JPA
private final JdbcTemplate jdbc; // raw SQL, same DataSource
OrderService(OrderRepository orders, JdbcTemplate jdbc) {
this.orders = orders;
this.jdbc = jdbc;
}
Order create(Order o) { return orders.save(o); } // JPA: easy CRUD
// JDBC: a grouped report that is awkward in JPQL
List<SalesRow> monthlySales() {
return jdbc.query(
"SELECT category, SUM(total) AS revenue FROM orders " +
"GROUP BY category ORDER BY revenue DESC",
(rs, i) -> new SalesRow(rs.getString("category"), rs.getBigDecimal("revenue")));
}
}
Both beans share the same HikariCP pool, so there is no conflict mixing them.
Common Mistakes & Pitfalls
- Thinking it's all-or-nothing: Learners believe choosing Spring Data JPA means never using JDBC, or that adding JdbcTemplate means abandoning JPA. In reality, professional Spring Boot apps routinely use JPA repositories for business logic and JdbcTemplate for specific queries — mixing is normal and encouraged.
- Overusing native queries in JPA: Beginners add @Query(nativeQuery=true) annotations when a query doesn't fit JPQL, then struggle with result mapping. Often, injecting JdbcTemplate and writing a RowMapper is cleaner than complex @SqlResultSetMapping configurations.
- Not recognizing when abstraction hurts: Learners force complex aggregation queries through JPA entities because that's what they learned, creating unreadable JPQL with multiple joins. They don't realize switching to SQL with JdbcTemplate would make the query clearer and faster.
Check your understanding
Answer these without looking back at the article.
- What is the N+1 problem in JPA and how does JDBC avoid it?
- Can you mix JdbcTemplate and Spring Data JPA in the same application?
- What is Spring Data JDBC and how is it different from JdbcTemplate?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.