Sunday, 6 February 2022

Helidon MP application using JPA With EclipseLink and Oracle ATP Database

In this blog post we will look at how to build a Helidon Microprofile application using JPA with Eclipselink as provider and Oracle Autonomous Database.

The code for this sample application is available at Github repo

Generate a Helidon MP application  using the maven archetype as follows:

mvn -U archetype:generate \
        -DinteractiveMode=false \
        -DarchetypeGroupId=io.helidon.archetypes \
        -DarchetypeArtifactId=helidon-bare-mp \
        -DarchetypeVersion=2.4.1 \
        -DgroupId=io.helidon.example \
        -DartifactId=helidon-jpa \
        -Dpackage=io.helidon.example.jpa

After creating the application, add following dependencies in your pom.xml:

        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
        </dependency>

        <dependency>
            <groupId>io.helidon.integrations.cdi</groupId>
            <artifactId>helidon-integrations-cdi-jta-weld</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.helidon.integrations.cdi</groupId>
            <artifactId>helidon-integrations-cdi-jpa</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.helidon.integrations.cdi</groupId>
            <artifactId>helidon-integrations-cdi-eclipselink</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>jakarta.transaction</groupId>
            <artifactId>jakarta.transaction-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8-production</artifactId>
            <version>21.1.0.0</version>
            <type>pom</type>
        </dependency>


If you want to use Oracle Autonomous database you need to create a cloud account and create Oracle ATP database. For further details refer to my earlier post on details regarding cloud account and ATP database creation.

The database schema used for this application is available at following Github repo.

Once the database schema creation and data scripts execution is completed, we can start with the application development.

Database Properties Configuration:

Create microprofile-config.properties file with following details:


In the above snapshot, the datasource url points to ATP database wallet location.

Eclipselink Configuration:

The eclipselink JPA provider configuration details are configured in persistence.xml.


Model: 

Create a POJO representing the JPA entity for COUNTRIES table. 



Now let's create a DAO class to implement few methods to perform operations on database table.


We are using a utility class to create the EntityManager instance by reading DB properties.


Now let's create a Resource class for supporting various REST endpoints as follows:


With this we are done with the code artifacts. Now let's build and run the application.

Use mvn package and generate the executable jar file. Once the artifact is generated, run the application.

java -jar helidon-jpa.jar

Now execute the REST endpoint using any client like CURL or Postman.



Happy Learning!!

No comments:

Post a Comment

Helidon MP application using JPA With EclipseLink and Oracle ATP Database

In this blog post we will look at how to build a Helidon Microprofile application using JPA with Eclipselink as provider and Oracle Autonomo...