Sunday 13 February 2022

Spring Data: ArangoTemplate: Get document by id or key

Spring Data ArangoOperations interface provides 'find' method to retrieve the document by id or key.

 

Signature

<T> Optional<T> find(Object id, Class<T> entityClass, DocumentReadOptions options) throws DataAccessException;
<T> Optional<T> find(Object id, Class<T> entityClass) throws DataAccessException;

 

Retrieves the document with the given id or key from a collection.

 

DocumentReadOptions’ is used to specify additional options while retrieving the document.

 

Below table summarizes the possible options associated with DocumentReadOptions.

 

Option

Data type

Description

ifNoneMatch

String

Document revision must not contain If-None-Match

ifMatch

String

document revision must contain If-Match

catchException

boolean

whether or not catch possible thrown exceptions

allowDirtyRead

boolean

If it is set to true then allows reading from followers in an active-failover setup.

streamTransactionId

String

If set, the operation will be executed within the transaction.

 

Example 

DocumentReadOptions docReadOptions = new DocumentReadOptions();
docReadOptions.allowDirtyRead(true);

Optional<Employee> empOptById = arangoTemplate.find(docId, Employee.class, docReadOptions);

 

Find the below working application.

 

Step 1: Create new maven project ‘arango-template-doc-by-id-or-key’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>arango-template-doc-by-id-or-key</artifactId>
    <version>1</version>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>com.arangodb</groupId>
            <artifactId>arangodb-spring-boot-starter</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Step 3: Define entity class.

 

Employee.java 

package com.sample.app.entity;

import com.arangodb.springframework.annotation.ArangoId;
import com.arangodb.springframework.annotation.Document;
import org.springframework.data.annotation.Id;

@Document("employees")
public class Employee {

    @Id // db document field: _key
    private String key;

    @ArangoId // db document field: _id
    private String arangoId;

    private Integer id;
    private String firstName;
    private String lastName;
    private Integer age;

    public Employee(Integer id, String firstName, String lastName) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Employee() {
        
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getArangoId() {
        return arangoId;
    }

    public void setArangoId(String arangoId) {
        this.arangoId = arangoId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Employee [key=" + key + ", arangoId=" + arangoId + ", id=" + id + ", firstName=" + firstName
                + ", lastName=" + lastName + ", age=" + age + "]";
    }

}

 

Step 4: Define arango configuration class.

 

ArangoConfig.java

package com.sample.app.config;

import org.springframework.context.annotation.Configuration;

import com.arangodb.ArangoDB;
import com.arangodb.springframework.annotation.EnableArangoRepositories;
import com.arangodb.springframework.config.ArangoConfiguration;

@Configuration
@EnableArangoRepositories(basePackages = { "com.sample.app" })
public class ArangoConfig implements ArangoConfiguration {

    @Override
    public ArangoDB.Builder arango() {
        return new ArangoDB.Builder().host("localhost", 8529).user("root").password("tiger");
    }

    @Override
    public String database() {
        return "abc_org";
    }
}

 

Step 5: Define main application class.

 

App.java

 

package com.sample.app;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.arangodb.entity.DocumentCreateEntity;
import com.arangodb.model.DocumentReadOptions;
import com.arangodb.springframework.core.ArangoOperations;
import com.arangodb.springframework.core.CollectionOperations;
import com.sample.app.entity.Employee;

@SpringBootApplication
public class App {

    @Autowired
    private ArangoOperations arangoTemplate;

    public static void main(String args[]) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    public CommandLineRunner demo() {

        return (args) -> {
            CollectionOperations empsCollection = arangoTemplate.collection(Employee.class);
            empsCollection.drop();

            Employee emp1 = new Employee(1, "Chamu", "M");

            DocumentCreateEntity docCreateEntity = (DocumentCreateEntity) arangoTemplate.insert(emp1);

            String docId = docCreateEntity.getId();
            String docKey = docCreateEntity.getKey();

            DocumentReadOptions docReadOptions = new DocumentReadOptions();
            docReadOptions.allowDirtyRead(true);

            Optional<Employee> empOptById = arangoTemplate.find(docId, Employee.class, docReadOptions);
            Optional<Employee> empOptByKey = arangoTemplate.find(docKey, Employee.class, docReadOptions);

            System.out.println("by id : " + empOptById.get());
            System.out.println("by key : " + empOptByKey.get());

        };
    }
}

 

Total project structure looks like below.



Run App.java, you will see below messages in console.

by id : Employee [key=42474, arangoId=employees/42474, id=1, firstName=Chamu, lastName=M, age=null]
by key : Employee [key=42474, arangoId=employees/42474, id=1, firstName=Chamu, lastName=M, age=null]

 

You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/arangodb/arango-template-doc-by-id-or-key

 

 

 

 

 


 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment