Using JPA or JDBC in Micronaut

When building data-driven applications, Micronaut Data allows you to choose between:

  • JPA (Java Persistence API)
  • JDBC (Direct SQL Mapping)

Both approaches are fully supported by Micronaut Data, but they serve different use cases.
In this post, we’ll explore both options, their differences, pros/cons, and how to configure them.

Using JPA or JDBC in Micronaut

πŸ” What is JPA?

JPA (Java Persistence API) is the standard API for object-relational mapping (ORM) in Java.
It allows you to map Java objects (entities) to database tables and automatically handles SQL generation.

  • βœ… Familiar if you’ve used Hibernate, Spring Data, Jakarta EE.
  • βœ… Full ORM β€” relationships, lazy loading, cascading, etc.
  • βœ… More abstraction, less SQL.

Micronaut supports JPA through its micronaut-data-jpa module.

πŸ” What is JDBC in Micronaut Data?

JDBC (Java Database Connectivity) uses Micronaut’s micronaut-data-jdbc module for more direct mapping.

  • βœ… Simple SQL mapping without full ORM.
  • βœ… Fast compile-time query generation.
  • βœ… Minimal runtime overhead.
  • βœ… Excellent for microservices where complex ORM features aren’t needed.

Micronaut Data JDBC focuses on compile-time SQL generation β€” no heavy runtime proxies like Hibernate.

βš– JPA vs JDBC: Quick Comparison

FeatureJPAJDBC
PerformanceSlower (runtime parsing)Faster (compile-time SQL)
ComplexityHigher (lazy loading, proxies)Lower
Learning CurveSteeperEasier
RelationshipsFull ORMLimited (manual handling)
Use caseComplex domainsMicroservices, high-performance apps

πŸ”§ Configuring JPA in Micronaut

Add Maven Dependencies

βœ… Micronaut Data uses Hibernate under the hood when JPA is selected.

Sample JPA Entity

βœ… Uses standard JPA annotations from jakarta.persistence.

Repository Interface (same as before!)

πŸ’‘ Even with JPA, Micronaut generates repository code at compile-time.

Enable JPA in application.yml

βœ… For production, you should disable hbm2ddl.auto and use migrations like Flyway or Liquibase.

πŸ”§ Configuring JDBC in Micronaut

Add Maven Dependencies

βœ… No Hibernate required!

Sample JDBC Entity

βœ… Uses @MappedEntity instead of @Entity.
βœ… Mapping is much lighter.

Repository Interface (same!)

βœ… Whether you use JPA or JDBC β€” repository code remains very similar.

Configure Datasource for JDBC (same as before)

πŸ§ͺ Which one should you choose?

If you need…Use
Full ORM, relationships, cascadingJPA
Simpler, faster, lightweight mappingJDBC
Large monolith enterprise systemsJPA
Modern microservices & cloud appsJDBC

🐞 Common Errors

ErrorCauseSolution
Hibernate exceptionsMissing Hibernate dependencyAdd micronaut-data-hibernate-jpa
Table not foundAuto-DLL disabledUse Flyway/Liquibase or enable auto
Field mapping issuesIncorrect annotationsUse correct annotations for JPA vs JDBC

🌐 External References

βœ… Summary

  • βœ… Micronaut supports both JPA and JDBC.
  • βœ… JDBC is lighter and faster.
  • βœ… JPA is full ORM and more powerful for complex domains.
  • βœ… Choose based on your project’s size and complexity.