Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3
Eclipse JNoSQL 1.1.3 simplifies NoSQL for Java with enhanced Redis, ArangoDB, Oracle NoSQL, and more. Easily switch databases using Jakarta specs.
Join the DZone community and get the full member experience.
Join For FreeNoSQL databases have become a cornerstone of modern application development, offering scalability and flexibility for handling diverse data types. However, for many Java developers, integrating with NoSQL databases can be complex and time-consuming. This is where Eclipse JNoSQL comes in, providing a seamless and standardized way to connect your Java applications to various NoSQL databases.
This article explores how Eclipse JNoSQL 1.1.3 simplifies database integration, enhances developer productivity, and offers flexibility across different databases. We’ll also walk you through a practical example using Quarkus and ArangoDB to demonstrate its features.
Why Java Developers Should Care About Eclipse JNoSQL
For many developers, working with NoSQL databases involves learning database-specific APIs, which can lead to fragmented, non-portable code. Eclipse JNoSQL eliminates these challenges by offering:
- Ease of Use: With intuitive annotations like
@Entity
,@Id
, and@Column
, you can map Java objects to NoSQL collections in seconds. - Flexibility: Switch between NoSQL databases (e.g., Redis, MongoDB, ArangoDB, Oracle NoSQL) without changing your application code.
- Standards Compliance: Implements the Jakarta Data and Jakarta NoSQL specifications, ensuring a future-proof, portable solution.
- Integration-Ready: Works seamlessly with Jakarta EE and MicroProfile components, leveraging CDI for dependency injection and configuration management.
What’s New in Eclipse JNoSQL 1.1.3
Eclipse JNoSQL has just released version 1.1.3, bringing new features, bug fixes, and performance improvements to its already robust ecosystem. This release is a significant milestone in smoothing Java developers’ interactions with NoSQL databases. In this article, we’ll explore the goals of Eclipse JNoSQL and the new features introduced in this version and provide a practical example of how to use it with Quarkus and ArangoDB.
Eclipse JNoSQL aims to simplify the integration of Java applications with NoSQL databases by providing a unified API and architecture. By adhering to Jakarta EE specifications, JNoSQL empowers developers to work seamlessly with various NoSQL databases — be they key-value, document, graph, or column-family databases.
- Interoperability: This abstraction abstracts database-specific complexities, making it easy to switch between databases like MongoDB, Redis, ArangoDB, and Oracle NoSQL.
- Specification-Driven: Implements Jakarta Data and Jakarta NoSQL specifications, offering a standardized way to work with NoSQL databases.
- Integration with Java Ecosystem: Leverages CDI for dependency injection and Eclipse MicroProfile for configuration, ensuring compatibility with modern Java frameworks like Quarkus and Spring Boot.
Limitations and Considerations for Eclipse JNoSQL
While Eclipse JNoSQL provides significant benefits for NoSQL database integration, it's essential to consider a few drawbacks to maintain a balanced perspective:
- Learning Curve for New Standards: Developers unfamiliar with Jakarta EE or MicroProfile may need time to understand and adapt to the specifications and APIs used by JNoSQL.
- Database-Specific Features: While JNoSQL abstracts common NoSQL operations, it may not fully support advanced, database-specific capabilities without additional customization.
- Community and Ecosystem: As a relatively specialized tool, its ecosystem and community support are smaller than broader Java frameworks like Hibernate or Spring Data.
- Performance Tuning: Generalized APIs may introduce a slight performance overhead compared to native, database-specific libraries, especially for high-performance applications.
Understanding these limitations helps developers make informed decisions and consider where JNoSQL best fits their projects.
Jakarta EE Specifications Supported By Eclipse JNoSQL
Eclipse JNoSQL supports Jakarta Data and Jakarta NoSQL, two critical specifications in the Jakarta EE ecosystem.
Jakarta Data
It provides a standard API for accessing and managing data repositories, enabling developers to perform CRUD operations easily. It focuses on simplifying pagination, sorting, and dynamic queries.
Jakarta NoSQL
It defines a uniform API for NoSQL database interactions. It provides annotations like @Entity
, @Id
, and @Column
for mapping Java classes to NoSQL collections and simplifying database operations.
These specifications reduce boilerplate code and promote portability across databases, making them essential for Java developers.
The latest release, version 1.1.3, focuses on improving reliability, security, and performance while introducing exciting new features:
- Redis: Enhanced support for Redis Sentinel for improved availability.
- ArangoDB: Security and key management updates.
- Oracle NoSQL: New credentials options for better integration.
- JNoSQL Lite: Added support for recording, improving traceability.
- Driver Updates: Improved drivers for various NoSQL databases.
- CDI Lite: Now it has support for Java record
To showcase the power of Eclipse JNoSQL 1.1.3, we’ll create a Quarkus application that manages “Goals” using ArangoDB. Thanks to the Jakarta EE specifications, switching to other NoSQL databases like MongoDB or Oracle NoSQL can be done with minimal changes.
Step-by-Step
Step 1: Setting Up Quarkus
Start your Quarkus project using the Quarkus Starter UI, selecting the following extensions:
- REST
- JSON
- ARC (CDI Lite)
- ArangoDB Driver
Your pom.xml
Should include:
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.jnosql</groupId>
<artifactId>quarkus-jnosql-document-arangodb</artifactId>
<version>3.3.2</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Step 2: Implementing Database Connectivity
Set your database connection in application.properties
:
jnosql.arangodb.host=localhost:8529
jnosql.document.database=goals
jnosql.keyvalue.database=goals
Run an ArangoDB instance locally using Docker:
docker run -e ARANGO_NO_AUTH=1 -d --name arangodb-instance -p 8529:8529 -d arangodb/arangodb
Create a database named goals
and a collection Goal
in the ArangoDB web UI.
Step 3: Modeling Your Data
Use Java’s record feature with CDI Lite:
import jakarta.json.bind.annotation.JsonbProperty;
import jakarta.json.bind.annotation.JsonbPropertyOrder;
import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;
import java.util.List;
@JsonbPropertyOrder({"id", "title", "description", "priority", "tasks"})
@Entity
public record Goal(
@Id @JsonbProperty("id") String id,
@JsonbProperty("title") @Column String title,
@JsonbProperty("description") @Column String description,
@JsonbProperty("priority") @Column int priority,
@JsonbProperty("tasks") @Column List<String> tasks) {}
Step 4: Setting Up a Repository
Define a repository using Jakarta Data:
@Repository
public interface NewYearWishes extends BasicRepository<Goal, String> {}
Step 5: Implementing a Service Layer
Create a service layer to manage business logic:
@ApplicationScoped
public class GoalService {
private final NewYearWishes newYearWishes;
@Inject
public GoalService(@Database(DatabaseType.DOCUMENT) NewYearWishes newYearWishes) {
this.newYearWishes = newYearWishes;
}
public List<Goal> findGoals(int page, int size) {
PageRequest pageRequest = PageRequest.ofPage(page).size(size);
return newYearWishes.findAll(pageRequest, Order.by(Sort.asc("priority"))).content();
}
public Goal save(Goal goal) {
return newYearWishes.save(goal);
}
public Optional<Goal> findById(String id) {
return newYearWishes.findById(id);
}
}
Step 6: Creating REST Endpoints
Define a REST resource:
@Path("/goals")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class GoalResource {
private final GoalService goalService;
@Inject
public GoalResource(GoalService goalService) {
this.goalService = goalService;
}
@GET
public List<Goal> goals(@QueryParam("page") int page, @QueryParam("size") int size) {
return goalService.findGoals(page, size);
}
@POST
public Goal create(Goal goal) {
return goalService.save(goal);
}
@GET
@Path("{id}")
public Goal findById(@PathParam("id") String id) {
return goalService.findById(id).orElseThrow(() -> new WebApplicationException("Goal not found", 404));
}
}
Step 7: Testing the Application
Here’s how to test your application using curl
:
curl -X POST -H "Content-Type: application/json" \
-d '{
"title": "Learn JNoSQL",
"description": "Master NoSQL with JNoSQL",
"priority": 1,
"tasks": ["Read documentation", "Write sample code"]
}' \
http://localhost:8080/goals
curl -X POST -H "Content-Type: application/json" \
-d '{
"title": "Get Fit",
"description": "Adopt a healthier lifestyle",
"priority": 2,
"tasks": ["Exercise daily", "Eat balanced meals", "Stay hydrated"]
}' \
http://localhost:8080/goals
curl -X POST -H "Content-Type: application/json" \
-d '{
"title": "Learn Quarkus",
"description": "Explore Quarkus framework features",
"priority": 3,
"tasks": ["Set up Quarkus", "Create REST endpoints", "Integrate JNoSQL"]
}' \
http://localhost:8080/goals
List Goals:
curl http://localhost:8080/goals?page=1&size=10
Conclusion
Eclipse JNoSQL 1.1.3 showcases its commitment to making NoSQL database interactions seamless for Java developers. Enhanced features and integration capabilities empower developers to build flexible and scalable applications. For more details, check out the complete example on GitHub.
Opinions expressed by DZone contributors are their own.
Comments