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"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

                <relativePath/> <!-- lookup parent from repository -->


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




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;
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;
@EnableArangoRepositories(basePackages = {"com.arangodb.spring.demo"})
public class DemoConfiguration implements ArangoConfiguration {
    public ArangoDB.Builder arango() {
        return new ArangoDB.Builder().host("localhost", 8529).user("root").password(null);
    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;
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() {
    public Character(final String name, final String surname, final boolean alive) {
        this.name = name;
        this.surname = surname;
        this.alive = alive;
    public Character(final String name, final String surname, final boolean alive, final Integer age) {
        this.name = name;
        this.surname = surname;
        this.alive = alive;
        this.age = age;
    // getter & setter
    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.