DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

UK-US Data Bridge: Join TechnologyAdvice and OneTrust as they discuss the UK extension to the EU-US Data Privacy Framework (DPF).

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • Architecture and Code Design, Pt. 1: Relational Persistence Insights to Use Today and On the Upcoming Years
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • Data Software Design Pitfalls on Java: Should We Have a Constructor on JPA?
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]

Trending

  • Java Collection Overhead
  • AWS Partition Projections: Enhancing Athena Query Performance
  • What Is CI/CD? Beginner’s Guide To Continuous Integration and Deployments
  • Building Your Own AI Chatbot With React and ChatGPT API
  1. DZone
  2. Data Engineering
  3. Data
  4. Making Your Life Easier Around Data With Java and Jakarta EE

Making Your Life Easier Around Data With Java and Jakarta EE

This article will cover more about the next steps of Jakarta EE around the world of data and its techniques to work as more besides just the data source.

Otavio Santana user avatar by
Otavio Santana
DZone Core CORE ·
Jun. 07, 22 · Review
Like (6)
Save
Tweet
Share
8.5K Views

Join the DZone community and get the full member experience.

Join For Free

There is no doubt about the importance of data around the application. We often talk about a stateless application, and it is the data where we delegate the state of the application. If the architecture is the mind, the information is the heart.

This article will cover more about the next steps of Jakarta EE around the world of data and its techniques to work as more besides just the data source. In other words, this is about how to apply practices around data such as repository and CQRS agnostically.

We have JPA for a while in the specification world to make your life easier in the relational database world. We're glad to work on a specification to make it easier to work with NoSQL: Jakarta NoSQL.

The core principle of the Jakarta NoSQL is to standardize behavior, to make a low cognitive load while changing among databases, and at the same time be extensible enough to allow specific behavior from a particular vendor. At this time we have the pleasure of announcing a new version, Jakarta NoSQL Version 1.0.0-b4.

Jakarta NoSQL Version 1.0.0-b4

This version has several library updates, bug fixes, and two new features. A builder pattern is included to create and combine conditions in the query to highlight one. Thus, Jakarta NoSQL has two approaches to creating a database request: using a fluent API and using a builder pattern. We won't cover the difference between that pattern here, but you can go further in my article entitled "Fluent-API: Creating Easier, More Intuitive Code With a Fluent API.".

To demonstrate this resource, we'll create a Worker entity where this Worker will have five attributes: id, name, city, age, and gender. We'll use MongoDB as a data source.

The first step is to have a MongoDB database running. You can make it easy with Docker by running the following command:

Shell
 
docker run -d --name mongodb-instance -p 27017:27017 mongo


The next step is to create a simple Java SE project where we'll include the minimum requirements for running the applications CDI, JSONB, and Eclipse Microprofile Configurations.

Furthermore, we'll include JNoSQL, the Jakarta NoSQL reference implementation. One thing new on this release is the mongodb-extension, where you can explore more particular behavior of this NoSQL vendor.

XML
 
 <dependency>
    <groupId>org.eclipse.jnosql.mapping</groupId>
    <artifactId>mongodb-extension</artifactId>
    <version>${project.version}</version>
</dependency>


For the application connection with the database, Jakarta NoSQL depends on Eclipse MicroProfile Configuration to explore more of the twelve factors, principally the configuration one.

We'll inject and make it available for CDI.

Java
 
@ApplicationScoped
public class MongoDBProducer {


    @Inject
    @ConfigProperty(name = "document")
    private DocumentCollectionManager manager;

    @Produces
    public MongoDBDocumentCollectionManager getManager() {
        return (MongoDBDocumentCollectionManager) manager;
    }

    public void destroy(@Disposes DocumentCollectionManager manager) {
        manager.close();
    }

}


We'll create the MongoDB connection. Without a password and user, the advantage of an overwritable configuration is that we can put several properties on production that do not make sense to run locally.

Properties files
 
document=document
document.database=olympus
document.settings.jakarta.nosql.host=localhost:27017
document.provider=org.eclipse.jnosql.communication.mongodb.document.MongoDBDocumentConfiguration


We have the project setup; then, we'll move the entity model with the Worker entity. Here, we can see the similarity with JPA. The specification tries to have resemblances, when possible, to have an accessible entrance for the Java developer.

Java
 
@Entity
public class Worker {

    @Id
    @Convert(ObjectIdConverter.class)
    private String id;

    @Column
    private String name;

    @Column
    private String city;

    @Column
    private int age;

    @Column
    private int dailyHours;

    @Column
    private Gender gender;
   
   //…
}


The next step is to create a specific query, where we'll combine boolean conditions, such as to give workers: 

  • I want to return a female older than thirty or a male more than thirty-five.
Java
 
public class App10 {

    public static void main(String[] args) {
        try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {

           Worker poliana = Worker.builder()
                   .age(30).name("Poliana")
                   .city("Salvador")
                   .gender(Gender.FEMALE)
                   .dailyHours(30).build();

            Worker otavio = Worker.builder()
                    .age(35).name("Otavio")
                    .city("Salvador")
                    .gender(Gender.MALE)
                    .dailyHours(30).build();

            DocumentTemplate template = container.select(DocumentTemplate.class).get();

            template.insert(Arrays.asList(otavio, poliana));

            DocumentCondition maleAfterThirty = and(eq("gender", Gender.MALE),
                    gte("age", 30));

            DocumentCondition femaleAfterThirty = and(eq("gender", Gender.FEMALE),
                    gte("age", 30));

            final DocumentQuery query =  DocumentQuery.builder()
                    .from("Worker")
                    .sort(asc("name"))
                    .where(or(maleAfterThirty, femaleAfterThirty))
                    .build();

            Stream<Worker> stream = template.select(query);
            List<Worker> workers = stream.collect(Collectors.toList());
            workers.forEach(System.out::println);

            template.delete(Worker.class, otavio.getId());
            template.delete(Worker.class, poliana.getId());
        }
    }


After several discussions to have more integration between JPA and NoSQL, the community has decided to create a new specification to apply those patterns around data: Jakarta Data.  We've published it to the Jakarta EE Committee. If you want to know more about it, please read my article entitled "Learn to Access Java Database With Jakarta Data."

When Will There Be a Final Version?

We're waiting for the new Jakarta Specification configuration. The proposal is to replace what we're using with MicroProfile Configuration and, hopefully, create compatibility with both. Until then, we don't have any other option than to wait.

Conclusion

Jakarta NoSQL is helping to engage the Java community. It is creating several discussions, such as the Jakarta Data specification, focusing on how critical it is to handle data, and starting several talks to make the Java Developer's life easier.

Database NoSQL Relational database Data (computing) Java (programming language) Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Architecture and Code Design, Pt. 1: Relational Persistence Insights to Use Today and On the Upcoming Years
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • Data Software Design Pitfalls on Java: Should We Have a Constructor on JPA?
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: