Developer Center » Languages » Java » Code Guides » Quarkus and Neo4j

Quarkus and Neo4j

All of the code shown is available in the quarkus-neo4j-intro-app Github repository.

This application uses a ready-to-use demo database with the goodreads data set containing Book and Author entities and the connections between them.

Connect to the database

To connect to Neo4j, you will need credentials and the connection details. Since this sample uses a public database, it will connect without any changes.

Connect to Neo4j – src/main/resources/application.properties

# Neo4j config
quarkus.neo4j.uri=neo4j+s://demo.neo4jlabs.com
quarkus.neo4j.authentication.username=goodreads
quarkus.neo4j.authentication.password=goodreadsCode language: PHP (php)

Domain classes

Domain classes represent the data in your domain. In this case, there will be a Book class and Author class.

Domain class – src/main/java/org/neo4j/Book.java

@NodeEntity
public class Book {
    @Id
    public String book_id;
    public String title;
    public String isbn;
    public String isbn13;

    @Relationship(value = "AUTHORED", direction = Relationship.Direction.INCOMING)
    public List<Author> authors = new ArrayList<>();

    public Book() {
    }

    public Book(String book_id, String title) {
        this.book_id = book_id;
        this.title = title;
    }
    
    //getters and setters
}Code language: Java (java)

The annotations are from the neo4j-ogm-quarkus-library to define the entity and properties for mapping data between Neo4j and the application. Another example is available in the library’s test source code. Note that the class above points to the missing Author class, which is created next.

Domain class – src/main/java/org/neo4j/Author.java

@NodeEntity
public class Author {
    @Id
    public String author_id;
    public String name;

    public Author() {
    }

    public Author(String author_id, String name) {
        this.author_id = author_id;
        this.name = name;
    }

    //getters and setters
}Code language: JavaScript (javascript)

Repository interfaces

Repository interfaces usually contain the methods and queries for retrieving and returning data between the application and the database.

Repository – src/main/java/org/neo4j/BookRepository.java

@ApplicationScoped
public class BookRepository {
    private final SessionFactory sessionFactory;

    BookRepository(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    Iterable<Book> findBooks() {
        return sessionFactory.openSession().query(Book.class, 
            "MATCH (book:Book)<-[rel:AUTHORED]-(author:Author) RETURN book, collect(rel), collect(author) LIMIT 10;", 
            Map.of());
    }

    Book findByTitle(String title) {
		return sessionFactory.openSession().queryForObject(Book.class, 
            "MATCH (book:Book)<-[rel:AUTHORED]-(author:Author) WHERE book.title = $title RETURN book, collect(rel), collect(author);", 
            Map.of("title", title));
	}

    Iterable<Book> findBooksByAuthor(String name) {
        return sessionFactory.openSession().query(Book.class, 
            "MATCH (book:Book)<-[rel:AUTHORED]-(author:Author) WHERE author.name = $name RETURN book, collect(rel), collect(author) LIMIT 10;", 
            Map.of("name", name));
    }
}Code language: Java (java)

A few methods define the following retrievals:

  1. Find 10 books – findBooks()
  2. Find a book by a title – findByTitle(String title)
  3. Find books by an author’s name – findBooksByAuthor(String name)

Controller/Resource classes

The controller (or resource in Quarkus terminology) will define the user-facing endpoints for retrieving data. Only the Book resource is defined for this application.

Resource – src/main/java/org/neo4j/BookResource.java

@RequestScoped
@Path("/books")
public class BookResource {
    private final BookRepository bookRepository;

    @Inject
    public BookResource(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Iterable<Book> getBooks() {
        return bookRepository.findBooks();
    }

    @GET
    @Path("/{title}")
    @Produces(MediaType.APPLICATION_JSON)
    public Book getBookByTitle(@PathParam("title") String title) {
        return bookRepository.findByTitle(title);
    }

    @GET
    @Path("/author/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public Iterable<Book> getBooksByAuthor(@PathParam("name") String name) {
        return bookRepository.findBooksByAuthor(name);
    }
}Code language: Java (java)

The first two annotations scope beans per request and set the main endpoint (/books). Each method is begun with a @GET annotation to define it as a GET endpoint. The last two methods use a path variable to pass in the book title or author name as a value on the URL path.

Run and test the application

You can run the application with the following command at a command line interface or some kind of play button provided in your preferred IDE.

./mvnw quarkus:devCode language: Shell Session (shell)

Here are a few requests to try. These examples are also available in the README of the Github repository.

http ":8080/books"

http ":8080/books/The Hobbit"

http ":8080/books/author/J.K. Rowling"Code language: Shell Session (shell)

Resources

Share Article