Using Java to Operate RavenDB
Using Java to operate RavenDB requires the use of RavenDB's Java client library. The following are the steps to operate RavenDB using Java, including Maven dependencies and complete Java code samples to implement data insertion, modification, query, and deletion.
Step:
1. Add RavenDB's Java client library dependencies to the Maven configuration file (pom. xml) of the project:
<dependency>
<groupId>net.ravendb</groupId>
<artifactId>ravendb-client</artifactId>
<version>4.2.5</version>
</dependency>
2. Create a client instance of RavenDB and configure the server URL and database name:
import net.ravendb.client.documents.DocumentStore;
import net.ravendb.client.documents.IDocumentStore;
import net.ravendb.client.documents.session.IDocumentSession;
public class RavenDBExample {
private IDocumentStore documentStore;
public void initialize() {
documentStore = new DocumentStore("http://localhost:8080", "your_database_name");
documentStore.initialize();
}
public void close() {
documentStore.close();
}
public static void main(String[] args) {
RavenDBExample example = new RavenDBExample();
example.initialize();
//Perform operations (insert, modify, query, delete)
example.close();
}
}
3. Perform operations (insert, modify, query, delete):
-Insert data:
public void insertData() {
try (IDocumentSession session = documentStore.openSession()) {
Person person = new Person();
person.setName("John Doe");
person.setAge(30);
session.store(person);
session.saveChanges();
}
}
-Modify data:
public void updateData() {
try (IDocumentSession session = documentStore.openSession()) {
Person person = session.load(Person.class, "persons/1-A");
person.setAge(40);
session.saveChanges();
}
}
-Query data (using Linq):
public void queryData() {
try (IDocumentSession session = documentStore.openSession()) {
List<Person> persons = session.query(Person.class)
.whereEquals("name", "John Doe")
.toList();
for (Person person : persons) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
-Delete data:
public void deleteData() {
try (IDocumentSession session = documentStore.openSession()) {
Person person = session.load(Person.class, "persons/1-A");
session.delete(person);
session.saveChanges();
}
}
Complete Java code example:
import net.ravendb.client.documents.DocumentStore;
import net.ravendb.client.documents.IDocumentStore;
import net.ravendb.client.documents.session.IDocumentSession;
import java.util.List;
public class RavenDBExample {
private IDocumentStore documentStore;
public void initialize() {
documentStore = new DocumentStore("http://localhost:8080", "your_database_name");
documentStore.initialize();
}
public void close() {
documentStore.close();
}
public void insertData() {
try (IDocumentSession session = documentStore.openSession()) {
Person person = new Person();
person.setName("John Doe");
person.setAge(30);
session.store(person);
session.saveChanges();
}
}
public void updateData() {
try (IDocumentSession session = documentStore.openSession()) {
Person person = session.load(Person.class, "persons/1-A");
person.setAge(40);
session.saveChanges();
}
}
public void queryData() {
try (IDocumentSession session = documentStore.openSession()) {
List<Person> persons = session.query(Person.class)
.whereEquals("name", "John Doe")
.toList();
for (Person person : persons) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
public void deleteData() {
try (IDocumentSession session = documentStore.openSession()) {
Person person = session.load(Person.class, "persons/1-A");
session.delete(person);
session.saveChanges();
}
}
public static void main(String[] args) {
RavenDBExample example = new RavenDBExample();
example.initialize();
example.insertData();
example.updateData();
example.queryData();
example.deleteData();
example.close();
}
}
Among them, the Person class is a Java class that represents the data you want to store, similar to the following definition:
public class Person {
private String id;
private String name;
private int age;
// Getters and setters
}