Persistence top

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:


1. Introduction

In this tutorial, we'll learn how to work with the Spring Data module and ArangoDB database. ArangoDB is a free and open-source multi-model database system. It supports key-value, document, and graph data models with one database core and a unified query language: AQL (ArangoDB Query Language).

We'll cover the required configuration, basic CRUD operations, custom queries, and entities relations.

2. Dependencies

To use Spring Data with ArangoDB in our application, we'll need the following dependency:


3. Configuration

Before we start working with the data, we need to set up a connection to ArangoDB. We should do it by creating a configuration class that implements the ArangoConfiguration interface:

public class ArangoDbConfiguration implements ArangoConfiguration {}

Inside we'll need to implement two methods. The first one should create ArangoDB.Builder object that will generate an interface to our database:

public ArangoDB.Builder arango() {
    return new ArangoDB.Builder()
      .host("", 8529)

There are four required parameters o create a connection: host, port, username, and password.

Alternatively, we can skip setting these parameters in the configuration class:

public ArangoDB.Builder arango() {
    return new ArangoDB.Builder();

As we can store them in resource file:

It's a default location for Arango to look for. It can be overwritten by passing an InputStream to a custom properties file:

InputStream in = MyClass.class.getResourceAsStream("");
ArangoDB.Builder arango = new ArangoDB.Builder()

The second method we have to implement is simply providing a database name that we need in our application:

public String database() {
    return "baeldung-database";

Additionally, the configuration class needs the @EnableArangoRepositories annotation that tells Spring Data where to look for ArangoDB repositories:

@EnableArangoRepositories(basePackages = {"com.baeldung"})

4. Data Model

As a next step, we'll create a data model. For this piece, we'll use an article representation with a name, author, and publishDate fields:

public class Article {

    private String id;

    private String arangoId;

    private String name;
    private String author;
    private ZonedDateTime publishDate;

    // constructors

The ArangoDB entity must have the @Document annotation that takes the collection name as an argument. By default, it's a decapitalize class name.

Next, we have two id fields. One with a Spring's @Id annotation and a second one with Arango's @ArangoId annotation. The first one stores the generated entity id. The second one stores the same id nut with a proper location in the database. In our case, these values could be accordingly 1 and articles/1.

Now, when we have the entity defined, we can create a repository interface for data access:

public interface ArticleRepository extends ArangoRepository<Article, String> {}

It should extend the ArangoRepository interface with two generic parameters. In our case, it's an Article class with an id of type String.

5. CRUD Operations

Finally, we can create some concrete data.

As a start point, we'll need a dependency to the articles repository:

ArticleRepository articleRepository;

And a simple instance of the Article class:

Article newArticle = new Article(
  "ArangoDb with Spring Data",
  "Baeldung Writer",

Now, if we want to store this article in our database, we should simply invoke the save method:

Article savedArticle =;

After that, we can make sure that both the id and arangoId fields were generated:


To fetch the article from a database, we'll need to get its id first:

String articleId = savedArticle.getId();

Then simply call the findById method:

Optional<Article> articleOpt = articleRepository.findById(articleId);

Having the article entity, we can change its properties:

Article article = articleOpt.get();
article.setName("New Article Name");;

Finally, invoke again the save method to update the database entry. It won't create a new entry because the id was already assigned to the entity.

Deleting entries is also a straightforward operation. We simply invoke the repository's delete method:


Delete it by the id is also possible:


6. Custom Queries

With Spring Data and ArangoDB, we can make use of the derived repositories and simply define the query by a method name:

public interface ArticleRepository extends ArangoRepository<Article, String> {
    Iterable<Article> findByAuthor(String author);

The second option is to use AQL (ArangoDb Query Language). It's a custom syntax language that we can apply with the @Query annotation.

Now, let's take a look at a basic AQL query that'll find all articles with a given author and sort them by the publish date:

@Query("FOR a IN articles FILTER == @author SORT a.publishDate ASC RETURN a")
Iterable<Article> getByAuthor(@Param("author") String author);

7. Relations

ArangoDB gives as a possibility to create relations between entities.

As an example, let's create a relation between an Author class and its articles.

To do so, we need to define a new collection property with @Relations annotation that will contain links to each article written by a given author:

@Relations(edges = ArticleLink.class, lazy = true)
private Collection<Article> articles;

As we can see, the relations in ArangoDB are defined through a separate class annotated with @Edge:

public class ArticleLink {

    private Article article;

    private Author author;

    // constructor, getters and setters

It comes with two fields annotated with @From and @To. They define the incoming and outcoming relation.

8. Conclusion

In this tutorial, we've learned how to configure ArangoDB and use it with Spring Data. We've covered basic CRUD operations, custom queries, and entity relations.

As always, all source code is available over on GitHub.

Persistence bottom
Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!