Spring Data Demo

Build a project with Maven

First we have to setup a project and add every needed dependency. For this demo we use Maven and Spring Boot.

We have to create a maven pom.xml:

        <?xml version="1.0" encoding="UTF-8"?>
        <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>

            <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.4.3</version>
                <relativePath/> <!-- lookup parent from repository -->
            </parent>

            <groupId>com.arangodb</groupId>
            <artifactId>spring-data-arangodb-tutorial</artifactId>
            <version>0.0.1-SNAPSHOT</version>

            <name>demo</name>
            <description>Demo project for Spring Boot</description>

            <properties>
                <java.version>11</java.version>
            </properties>

            <dependencies>
                <dependency>
                    <groupId>com.arangodb</groupId>
                    <artifactId>arangodb-spring-data</artifactId>
                    <version>3.6.0</version>
                </dependency>
                <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>
    

Create an Application class

After we have ensured that we can fetch all the necessary dependencies, we can create our first classes.

The class DemoApplication is our main class where we will later add certain CommandLineRunner instances to be executed.

package com.arangodb.spring.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class DemoApplication {
    public static void main(final String... args) {
        Class[] runner = new Class[]{};
        System.exit(SpringApplication.exit(SpringApplication.run(runner, args)));
    }
}

Create a Configuration class

We also need a configuration class to setup everything to connect to our ArangoDB instance and to declare that all needed Spring Beans are processed by the Spring container.

  • @EnableArangoRepositories Defines where Spring can find your repositories
  • arango() Method to configure the connection to the ArangoDB instance
  • database() Method to define the database name
package com.arangodb.spring.demo;
 
import com.arangodb.ArangoDB;
import com.arangodb.springframework.annotation.EnableArangoRepositories;
import com.arangodb.springframework.config.ArangoConfiguration;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@EnableArangoRepositories(basePackages = {"com.arangodb.spring.demo"})
public class DemoConfiguration implements ArangoConfiguration {
 
    @Override
    public ArangoDB.Builder arango() {
        return new ArangoDB.Builder().host("localhost", 8529).user("root").password(null);
    }
 
    @Override
    public String database() {
        return "spring-demo";
    }
}

Data modeling

Lets create our first bean which will represent a collection in our database. With the @Document annotation we define the collection as a document collection. In our case we also define the alternative name characters for the collection. By default the collection name is determined by the class name. @Document also provides additional options for the collection which will be used at creation time of the collection.

Because many operations on documents require a document handle, it’s recommended to add a field of type String annotated wit @Id to every entity. The name doesn’t matter. It’s further recommended to not set or change the id by hand.

package com.arangodb.spring.demo.entity;
 
import com.arangodb.springframework.annotation.Document;
import org.springframework.data.annotation.Id;
 
@Document("characters")
public class Character {
 
    @Id // db document field: _key
    private String id;
 
    @ArangoId // db document field: _id
    private String arangoId;
 
    private String name;
    private String surname;
    private boolean alive;
    private Integer age;
 
    public Character() {
        super();
    }
 
    public Character(final String name, final String surname, final boolean alive) {
        super();
        this.name = name;
        this.surname = surname;
        this.alive = alive;
    }
 
    public Character(final String name, final String surname, final boolean alive, final Integer age) {
        super();
        this.name = name;
        this.surname = surname;
        this.alive = alive;
        this.age = age;
    }
 
    // getter & setter
 
    @Override
    public String toString() {
        return "Character [id=" + id + ", name=" + name + ", surname=" + surname + ", alive=" + alive + ", age=" + age + "]";
    }
 
}

What's next

In the next part of our Spring Data demo, we’re going to take a look at how to perform CRUD operations and how to search for documents using the query by example API provided by Spring Data.