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!!

Wednesday 26 January 2022

Simple JDBC program to connect with Oracle Autonomous Database

This blog post is about how to connect to Oracle Autonomous database using JDBC.

The prerequisites for this are you need a free Oracle cloud account and create a Oracle Autonomous database which is always free.

1. Create a free Oracle cloud acount

2.Create free Oracle database account

After performing above 2 steps you can download the wallet zip file from the ATP database and keep it in a folder. (Refer to this link for dowloading the wallet)

After downloading the wallet, extract the files to a folder of your choice. Create folder by name "network" in the extracted folder and create "admin" folder in the "network" folder.

Copy cwallet.sso, ojdbc.properties and tnsnames.ora to admin folder.

Now open eclipse and create a New Java project.

Download the ojdbc drivers from this location. Extract the jars and copy them into a folder and add them in the Classpath of the Java project.



Now write the java class to invoke the database.


Run the class and you should see the output as follows:

Driver Name: Oracle JDBC driver
Driver Version: 19.13.0.0.0
Database Username is: ADMINUSER

Happy learning!!




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...