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



    io.micronaut.data
    micronaut-data-hibernate-jpa



    org.hibernate.orm
    hibernate-core


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

Sample JPA Entity


package com.kscodes.micronaut.school.entities;

import jakarta.persistence.*;
import java.time.LocalDate;

@Entity
@Table(name = "students")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    private LocalDate dateOfBirth;

    private String email;

    // Getters and Setters
}

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

Repository Interface (same as before!)


package com.kscodes.micronaut.school.repositories;

import com.kscodes.micronaut.school.entities.Student;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface StudentRepository extends CrudRepository {
}

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

Enable JPA in application.yml


jpa:
  default:
    properties:
      hibernate:
        hbm2ddl:
          auto: update

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

πŸ”§ Configuring JDBC in Micronaut

Add Maven Dependencies



    io.micronaut.data
    micronaut-data-jdbc


βœ… No Hibernate required!

Sample JDBC Entity


package com.kscodes.micronaut.school.entities;

import io.micronaut.data.annotation.*;

import java.time.LocalDate;

@MappedEntity("students")
public class Student {

    @Id
    @GeneratedValue(GeneratedValue.Type.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    private LocalDate dateOfBirth;

    private String email;

    // Getters and Setters
}

βœ… 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)


datasources:
  default:
    url: jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    username: sa
    driverClassName: org.h2.Driver
    dialect: H2

πŸ§ͺ 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.