Skip to content

Latest commit

 

History

History

tutorial

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

ArangoDB-Logo

Spring Data ArangoDB - Tutorial

This is a tutorial on how to configure Spring Data ArangoDB, without using Spring Boot Starter ArangoDB. A more extensive demo about the features of Spring Data ArangoDB can be found in the Spring Boot Starter ArangoDB Demo.

Get started

This tutorial is about how to configure Spring Data ArangoDB without using Spring Boot Starter ArangoDB.

For a more extensive tutorial about the features of Spring Data ArangoDB and Spring Boot support, see the Spring Boot Starter documentation.

Build a project with Maven

Set up a project and add every needed dependency. This demo uses Maven and Spring Boot.

Create a Maven pom.xml:

<?xml version="1.0" encoding="UTF-8"?> <projectxmlns="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> <relativePath/> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.0</version> </parent> <groupId>com.arangodb</groupId> <artifactId>spring-data-arangodb-tutorial</artifactId> <version>1.0.0</version> <name>spring-data-arangodb-tutorial</name> <description>ArangoDB Spring Data Tutorial</description> <properties> <java.version>21</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.arangodb</groupId> <artifactId>arangodb-spring-data</artifactId> <version>4.5.0</version> </dependency> </dependencies> </project>

Substitute the versions with the latest available versions that are compatible. See the Supported versions for details.

Entity classes

For this tutorial we will model our entity with a Java record class:

@Document("characters") publicrecordCharacter( @IdStringid, Stringname, Stringsurname ) { }

Create a repository

Now that we have our data model, we want to store data. For this, we create a repository interface which extends ArangoRepository. This gives us access to CRUD operations, paging, and query by example mechanics.

publicinterfaceCharacterRepositoryextendsArangoRepository<Character, String> { }

Create a Configuration class

We need a configuration class to set up 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
  • returnOriginalEntities(): Method to configures the behavior of repository save methods to either return the
    original entities (updated where possible) or new ones. Set to false to use java records.
@Configuration@EnableArangoRepositories(basePackages = {"com.arangodb.spring.demo"}) publicclassAdbConfigimplementsArangoConfiguration { @OverridepublicArangoDB.Builderarango() { returnnewArangoDB.Builder() .host("localhost", 8529) .user("root") .password("test"); } @OverridepublicStringdatabase() { return"spring-demo"; } @OverridepublicbooleanreturnOriginalEntities() { returnfalse; } }

Note that, in case the driver is configured to use a protocol with VPACK content type (i.e. HTTP_VPACK or HTTP2_VPACK), then the ArangoConfiguration#contentType() method must be overridden to return ContentType.VPACK as shown in the following example:

@OverridepublicArangoDB.Builderarango() { newArangoDB.Builder() // ...  .protocol(Protocol.HTTP2_VPACK); } @OverridepublicContentTypecontentType() { returnContentType.VPACK; }

Create a CommandLineRunner

To run our demo as command line application, we have to create a class implementing CommandLineRunner:

@ComponentScan("com.arangodb.spring.demo") publicclassCrudRunnerimplementsCommandLineRunner { @AutowiredprivateArangoOperationsoperations; @AutowiredprivateCharacterRepositoryrepository; @Overridepublicvoidrun(String... args) { // first drop the database so that we can run this multiple times with the same datasetoperations.dropDatabase(); System.out.println("# CRUD operations"); // save a single entity in the database// there is no need of creating the collection first. This happen automaticallyCharacternedStark = newCharacter(null, "Ned", "Stark"); Charactersaved = repository.save(nedStark); System.out.println("Ned Stark saved in the database: " + saved); } }

Run the application

Finally, we create a main class:

@SpringBootApplicationpublicclassDemoApplication { publicstaticvoidmain(finalString... args) { System.exit(SpringApplication.exit( SpringApplication.run(CrudRunner.class, args) )); } }

And run it with:

mvn spring-boot:run

This should produce a console output similar to:

Ned Stark saved in the database: Character[id=2029, name=Ned, surname=Stark] 
close