Skip to content

8. 7 - Lab 5 Exploring Jakarta Data

8.1 Step 1: Make sure that the database is running.

Check the database setup at: https://github.com/o-s-expert/ddd-workshop-labs?tab=readme-ov-file#database-setup

8.2 Step 2: Add the Jakarta Data dependency

At the pom.xml file at the properties section add the following dependency:

<jnosql.version>1.1.10</jnosql.version>

On the dependencies section add the following dependency:

      <!--  <dependency>
            <groupId>org.eclipse.jnosql.databases</groupId>
            <artifactId>jnosql-mongodb</artifactId>
            <version>${jnosql.version}</version>
        </dependency>-->
<dependency>
    <groupId>org.eclipse.jnosql.databases</groupId>
    <artifactId>jnosql-oracle-nosql</artifactId>
    <version>${jnosql.version}</version>
</dependency>

Here we have the option of two databases:

Oracle NoSQL and MongoDB.

At the microprofile-config.properties file add the following properties:

#JNoSQL configuration
jnosql.keyvalue.database=products
jnosql.document.database=products

# Oracle NoSQL properties
jnosql.oracle.nosql.host=http://localhost:8080
# MongoDB properties
jnosql.mongodb.url=mongodb://localhost:27017

8.3 Step 3: Update the Repository

This step will update the annotations using the Jakarta Data API, thus at the ProductRepository update the code:

@Repository
public interface ProductRepository {

    @Save
    Product save(Product product);

    Optional<Product> findById(String id);

    List<Product> findAll();

    void deleteById(String id);

}

You can either remove the InMemoryProductRepository the whole class or just the @Repository annotation.

8.4 Step 4: Update the Model domain

Update the Product class to use the Jakarta NosQL annotations:

import jakarta.nosql.Column;
import jakarta.nosql.Embeddable;

import java.math.BigDecimal;

@Embeddable(Embeddable.EmbeddableType.GROUPING)
public record Money(@Column String currency, @Column BigDecimal amount) {
}
package com.example.ecommerce.domain;


import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.math.BigDecimal;
import java.util.Objects;

@Entity
public class Product {

    @Id
    private String id;

    @Column
    private String name;
    @Column
    private Money price;


    Product(String id, String name, Money price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    Product() {
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Money getPrice() {
        return price;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "Product{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public static ProductBuilder builder() {
        return new ProductBuilder();
    }

    public void update(BigDecimal amount) {
        this.price = new Money(price.currency(), amount);
    }
}

Last update: 2025-09-05