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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • Solving Comprehension Debt in AI-Generated Codebases
  • RAG Architecture Patterns: Proxy, Caching, and Routing
  • When 12 Hours of Offline Changes Collide: Designing Conflict-Safe Mobile Sync
  • When Production Stops Moving: Running Claude Code Across a Distributed Enterprise Integration Team

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. An Introduction to RavenDB With Java

An Introduction to RavenDB With Java

This article will look at about how to install RavenDB and how to integrate it with Java using Eclipse JNoSQL.

By 
Otavio Santana
DZone Core CORE ·
Jul. 03, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

RavenDB is a NoSQL document type that is written in C#, and it is easy to install and configure a fully transactional NoSQL. Also, it has a terrific UI, which allows reading performance statistics, live monitoring, deep insight into database internals, and extensive logging and incident reporting. This article will look at about how to install it and how to integrate it with Java using Eclipse JNoSQL.

RavenDB is an intuitive and multiplatform database, thereby, it has support to Windows, Linux, Docker, and even on the Py with several clients language that includes Java. The robust feature at RavenDB is indisputably the UI, which is intuitive and easy to use even to admin operations such as log and set configurations that a Java developer can use online from here.

Installing RavenDB Using Docker

To install RavenDB using docker follows the step below:

  1. Install docker: https://www.docker.com/
  2. Run docker command
docker run -d -p 8080:8080 -p 38888:38888 ravendb/ravendb

3. Go to: http://localhost:8080/

4. Accept the terms

5. Follow the RavenDB Setup Wizard (to test, you might choose an unsecured option)

6. Create a database "developers"

Image title

The next step is to configure the application to use it.

Setting the Application Dependencies

The next step is to configure a smooth Java SE application with CDI and Eclipse JNoSQL with RavenDB. Internally, the communication — aka Diana — layer does not create its own communication; it uses the RavenDB client and uses the Communication API as an adapter. This demo uses a Maven project that needs to set the dependencies beyond CDI 2.0 implementation. It needs the Eclipse JNosQL dependencies as shown below:

<dependencies>
    <dependency>
        <groupId>org.jnosql.artemis</groupId>
        <artifactId>artemis-document</artifactId>
        <version>${jnosql.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jnosql.diana</groupId>
        <artifactId>ravendb-driver</artifactId>
        <version>${jnosql.version}</version>
    </dependency>
</dependencies>

Equally important is to do a DocumentCollectionManager; eligible to CDI setting an instance as producer.

@ApplicationScoped
public class DocumentCollectionManagerProducer {

    private static final String COLLECTION = "developers";

    private RavenDBDocumentConfiguration configuration;

    private DocumentCollectionManagerFactory managerFactory;

    @PostConstruct
    public void init() {
        configuration = new RavenDBDocumentConfiguration();
        Settings settings = Settings.builder()
          .put("ravendb-server-host-1", "http://localhost:8080")
          .build();
        managerFactory = configuration.get(settings);
    }


    @Produces
    public DocumentCollectionManager getManager() {
        return managerFactory.get(COLLECTION);

    }

}

This sample will use a natural model to a developer who is a person and has a job and an address.

@Entity
public class Person {

  @Id
  private String id;

  @Column
  private String name;

  @Column
  private List<String> phones;

  @Column
  private Integer age;

  @Column
  private Address address;

  @Column
  private Job job;
}  

@Entity
public class Job {

  @Column
  private double salary;

  @Column
  private String ocupation;
}

@Entity
public class Address {

  @Column
  private String street;

  @Column
  private String city;
}

The model and the configuration are ready, and it is time to create the class that will run this demo.

public class App {


    public static void main(String[] args) {

        try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {

            Address address = new Address("Av nove de julho", "São Paulo");
            Job job = new Job(12.12, "Developer");
            Person person = Person.builder().
                    withPhones(Arrays.asList("234", "432"))
                    .withName("Ada Lovelace")
                    .withAddress(address)
                    .withAge(30)
                    .withJob(job)
                    .build();
            DocumentTemplate template = container.select(DocumentTemplate.class).get();
            Person saved = template.insert(person);
            System.out.println("Person saved" + saved);


            DocumentQuery query = select().from("Person")
                    .where("_id").eq(person.getId()).build();

            Optional<Person> personOptional = template.singleResult(query);
            System.out.println("Entity found: " + personOptional);

        }
    }

    private App() {
    }
}

Image title

To make that integration easier, there is the repository that handles the implementation of the methods that do exist in the Repository interface and new methods once its method query convention.

public interface PersonRepository extends Repository<Person, Long> {
  List<Person> findByName(String name);
}


public class App2 {


public static void main(String[] args) {

    try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {

        Address address = new Address("10880 Malibu Point", "Malibu");
        Job job = new Job(1_000, "Rich");
        Person person = Person.builder().
                withPhones(Arrays.asList("9999999", "55555"))
                .withName("Tony stark")
                .withAddress(address)
                .withAge(50)
                .withJob(job)
                .build();
        PersonRepository repository = container.select(PersonRepository.class)
                .select(DatabaseQualifier.ofDocument()).get();
        repository.save(person);
        List<Person> people = repository.findByName("Tony stark");
        System.out.println("Entity found: " + people);
    }
}

    private App2() {
    }
}

Also, there is the text as String that came from the version 0.0.6. A Java developer can query as text from both Repository and DocumentTemplate.

public interface PersonRepository extends Repository<Person, Long> {

    List<Person> findByName(String name);

    @Query("select * from Person where age > @age")
    List<Person> findByAge(@Param("age") Integer age);
}

public class App3 {

public static void main(String[] args) {

    try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
        DocumentTemplate template = container.select(DocumentTemplate.class).get();
        PersonRepository repository = container.select(PersonRepository.class)
                .select(DatabaseQualifier.ofDocument()).get();
        PreparedStatement prepare = template.prepare("select * from Person where name = @name");
        prepare.bind("name", "Tony stark");
        List<Person> people = prepare.getResultList();
        System.out.println("Person from name: " + people);
        System.out.println("Person from age: " + repository.findByAge(30));
    }
 }
}

This post covered how easy it is to use RavenDB; a document NoSQL database that has a fantastic UI.

RavenDB code sample: https://github.com/JNOSQL/artemis-demo/tree/master/artemis-demo-java-se/ravendb

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook