micronaut

Introduction to Micronaut Data!

In this article is to answer of what is micronaut data? why it is there and how to use it?

If you are new to the micronaut then kindly read the below article to get started with it. https://www.techwasti.com/micronaut-java-full-stack-microservice-framework/

What is Micronaut Data?

Micronaut Data is a productivity-boosting open-source technology that dramatically improves runtime and memory performance of data access repository logic for the microservice and serverless era. —Ref Micronaut Doc

Micronaut data is basically the one of the project from the micronaut family. This is a data access toolkit from micronaut framework. The purpose of this project to do operations on databases easily and seamlessly.

Micronaut Data is a database access toolkit that uses Ahead of Time (AoT) compilation to pre-compute queries for repository interfaces that are then executed by a thin, lightweight runtime layer.

Micronaut Data is inspired by Spring data and GROM. Both these toolkits are created milestones in the java world. Micronaut data learned from GROM and spring data and created enhanced toolkit to access the database. Below are some of the features:

  1. No query translation:- Micronaut does this by the Micronaut compiler at compilation time. Both GORM and Spring Data use regular expressions and pattern matching in combination with runtime generated proxies to translate a method definition on a Java interface into a query at runtime.
  2. Type Safety:- Micronaut make sure repository methods implemented and failed at compile time only.
  3. No Reflection or Runtime Proxies:- If you check the core thing differs between micronaut and spring is in the usage of runtime proxies and reflection API. Micronaut is not using runtime proxies, resulting in better performance, smaller stack traces and reduced memory consumption due to a complete lack of reflection caches.
  4. No runtime model:- Both GORM and Spring Data maintain a runtime meta-model that uses reflection to model relationships between entities. The problem is worse when combined with Hibernate which maintains its own meta-model as you end up with duplicate meta-models.
  5. Backward compatible:- Micronaut Data provides a general API for translating a compile time Query model into a query at compilation time and provides runtime support to JPA, hibernate and jdbc.

Example time:-

package com.techwasti.micronautdata.demo;

import io.micronaut.context.annotation.Executable;
import io.micronaut.data.annotation.*;
import io.micronaut.data.model.*;
import io.micronaut.data.repository.CrudRepository;
import java.util.List;

@Repository 
interface UserRepository extends CrudRepository<User, Long> { 
    @Executable
    User find(String name);
}

@Repository annotation states that this is a data repository and this will provide a compile-time implementation for this. By extending CrudRepository we are inheriting the common CRUD methods (create, read, update and delete).

If you are aware about the spring framework then there is almost semantics are similar. Micronaut taken inspiration from the spring framework only it differs in the underling implementation.

How Micronaut Data works?

Micronaut Data computes the query for the find method automatically at compilation time making it available at runtime via annotation metadata. Micronaut Data uses two key features of micronaut:

  1. TypeElementVisitor API.
  2.  Introduction Advice.

Micronaut Data defines a RepositoryTypeElementVisitor that at compilation time visits all interfaces from source that are annotated with the @Repository annotation.

How to use it?

To use it in your project we need to add dependency in our beloved build tool either maven or gradle. Below are the dependency if you want to use micronaut data and JPA.

The build time annotation processors:

Maven:

<annotationProcessorPaths>
    <path>
        <groupId>io.micronaut.data</groupId>
        <artifactId>micronaut-data-processor</artifactId>
        <version>2.3.1</version>
    </path>
</annotationProcessorPaths>

Gradle:

annotationProcessor("io.micronaut.data:micronaut-data-processor:2.3.1")

The runtime APIs:

You should then configure a runtime dependency that matches the implementation you wish to use and ensure the implementation configured properly.

Maven:

<dependency>
    <groupId>io.micronaut.data</groupId>
    <artifactId>micronaut-data-hibernate-jpa</artifactId>
    <version>2.3.1</version>
</dependency>

Gradle:

implementation("io.micronaut.data:micronaut-data-hibernate-jpa:2.3.1")

Coding time:

Let us go ahead check and makes our hands dirty.

We can create sample entity and repository in following way.

package com.techwasti.micronautdata.demo;

import javax.persistence.*;

@Entity
public class User {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public User() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

In the above code snippet we just defined the entity by using @Entity annotation. Now let us create repository.

package com.techwasti.micronautdata.demo;

import io.micronaut.context.annotation.Executable;
import io.micronaut.data.annotation.*;
import io.micronaut.data.model.*;
import io.micronaut.data.repository.CrudRepository;
import java.util.List;

@Repository 
interface UserRepository extends CrudRepository<User, Long> { 
    @Executable
    User find(String name);
}

Now our bean is ready you can inject this bean at service layer so that we can access the methods and able to do CRUD operations.

@Inject
UserRepository userRepository;

Above is where you are injecting the repository object into service so that you can access it and does the data manipulation. Retrieve Record from database.

User user = userRepository.findById(id).orElse(null);

Above code snippet will return the user data if it exist or else null.

Configure the build:-

As above we stated that the micronaut does most of the work at compile time so to make it work and test in local environment we have to setup our build properly.

There are two imporant aspects to Micronaut Data:

  1. The build time annotation processors
  2. The runtime APIs

As above I have mentioned about both gradle and maven compile time annotation processors and runtime APIs.

Conclusion:-

In this article we discussed about the micronaut data toolkit, in that we put light on how it works why we need annotation processor and runtime APIs. How it different from spring data and GORM and still it is supporting JPA hibernate and JDBC and at the final we checked some code snippet. Let me know your understanding about this.