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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Trending

  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • Driving DevOps With Smart, Scalable Testing
  • Ensuring Configuration Consistency Across Global Data Centers
  • Navigating the LLM Landscape: A Comparative Analysis of Leading Large Language Models
  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 user avatar
Otavio Santana
DZone Core CORE ·
Jul. 03, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!