© 2023 Neo4j, Inc. All rights reserved.
© 2023 Neo4j, Inc. All rights reserved.
1
Spring Data Neo4j
Concepts & Application Development
Gerrit Meier and Michael Simons,
SDN and OGM team @ Neo4j
© 2023 Neo4j, Inc. All rights reserved.
Who are we?
Michael Simons Gerrit Meier
© 2023 Neo4j, Inc. All rights reserved.
Who are you?
• Java Developer
• Graph Specialist
• Where are you from?
• …
© 2023 Neo4j, Inc. All rights reserved.
Today’s plan
• Get your development environment set up (If not already done)
• Create Neo4j Java Driver-based Spring Boot applications
• Concepts and Architecture of Spring Data Neo4j
• Advance to a Spring Data Neo4j application
◦ Modelling
◦ Repositories
• Control what gets loaded
• Custom queries and projections
• Working with Neo4jTemplate
• Use Neo4jTemplate features
• Testing
• Add tests to your application
© 2023 Neo4j, Inc. All rights reserved.
Development environment
• (free) Aura instance running
• JDK 17
◦ Driver and Spring Boot
• IDE
• Allow access to download artifacts from Maven Central
• Docker for testing
© 2023 Neo4j, Inc. All rights reserved.
Database preparation
© 2023 Neo4j, Inc. All rights reserved.
Spring Boot application
• start.spring.io
• no dependencies
• Maven/Gradle choose
◦ Choose what you are comfortable with
◦ Will use Maven
© 2023 Neo4j, Inc. All rights reserved.
8
Live coding / code along
Project setup
© 2023 Neo4j, Inc. All rights reserved.
© 2023 Neo4j, Inc. All rights reserved.
9
Practical part
Create a Spring Boot application
using the Neo4j Java Driver.
Todo:
• Add Neo4j Java
Driver
• Configuration in
application.properties
• Implement
CommandLineRunner
• Output all Movie-node
titles
© 2023 Neo4j, Inc. All rights reserved.
Spring Data Neo4j Architecture
Neo4j Java Driver spring-boot-autoconfigure
provides
© 2023 Neo4j, Inc. All rights reserved.
Spring Data Neo4j Architecture
Neo4j Java Driver
spring-boot-starter-data-neo4j
spring-boot-autoconfigure
provides
provides
Spring Data Neo4j
© 2023 Neo4j, Inc. All rights reserved.
Spring Data Neo4j Architecture
Spring Data Repositories
Neo4j Template
Neo4j Client
Neo4j Java Driver
spring-boot-starter-data-neo4j
spring-boot-autoconfigure
provides
provides
© 2023 Neo4j, Inc. All rights reserved.
Spring Data Neo4j Architecture
Spring Data Repositories
Neo4j Template
Neo4j Client
Neo4j Java Driver
spring-boot-starter-data-neo4j
spring-boot-autoconfigure
provides
provides
© 2023 Neo4j, Inc. All rights reserved.
@Node("Person")
public class Actor {
@Id
public final String name;
@Relationship("ACTED_IN")
public final List<Movie> movies;
public Actor(String name, List<Movie> movies) {
this.name = name;
this.movies = movies;
}
}
@Node
public class Movie {
@Id
public final String title;
public Movie(String title) {
this.title = title;
}
}
© 2023 Neo4j, Inc. All rights reserved.
15
Live coding / code along
Spring Data Neo4j (starter) dependency
Model domain classes
First repository
© 2023 Neo4j, Inc. All rights reserved.
© 2023 Neo4j, Inc. All rights reserved.
16
Practical part
Enrich the application by a
PersonRepository with additional
functionality.
Todo:
• Create a repository
for the Person
• Return Tom Hanks
with all Movies, he
acted in.
© 2023 Neo4j, Inc. All rights reserved.
@Node
public class Person {
@Id
public final String name;
public Person(String name) {
this.name = name;
this.movies = movies;
}
}
@Node
public class Movie {
@Id
public final String title;
@Relationship(type="ACTED_IN", direction=
Relationship.Direction.INCOMING)
public List<Person> actors;
@Relationship(type="DIRECTED", direction=
Relationship.Direction.INCOMING)
public List<Person> directors;
@Relationship(type="PRODUCED", direction=
Relationship.Direction.INCOMING)
public List<Person> producers;
public Movie(String title) {
this.title = title;
}
}
© 2023 Neo4j, Inc. All rights reserved.
More on repositories
Custom queries
• Form your fetch shape by hand
© 2023 Neo4j, Inc. All rights reserved.
More on repositories
Projections
• Let SDN form your fetch shape
© 2023 Neo4j, Inc. All rights reserved.
© 2023 Neo4j, Inc. All rights reserved.
20
Practical part
Use existing functionality in
Spring Data Neo4j to load only the
data you are interested in.
Todo:
• Custom query:
Find a Movie by title
with just the actors
attached
• Projection:
Find a Movie by title
with just the actors
(additional
PersonProjection)
© 2023 Neo4j, Inc. All rights reserved.
Pitfalls of Sub-Graph loading
• Loaded slice is incomplete
◦ SDN takes the object (graph) to persist as the only truth
◦ Relationships will get removed
• Open projections (DTOs) will adapt entity metadata and fall back to
original query creation
◦ Also in multi-level projections starting with interfaces
• Use-case specific projections
◦ A lot of interfaces/classes for each use-case
© 2023 Neo4j, Inc. All rights reserved.
22
Live coding / code along
Neo4jTemplate with Projections
© 2023 Neo4j, Inc. All rights reserved.
© 2023 Neo4j, Inc. All rights reserved.
23
Practical part
Use projections for save
operations.
Todo:
• Load Movie via
Neo4jTemplate the
Movie -> Actor
projection
• Edit the Movie
(release year) and call
save
• Ensure that all
relationships still exist
© 2023 Neo4j, Inc. All rights reserved.
Testing
• Testslice @DataNeo4jTest
◦ Set up transaction manager, repositories, etc.
• Testcontainers Neo4j
◦ Docker based solution to run integration tests
© 2023 Neo4j, Inc. All rights reserved.
© 2023 Neo4j, Inc. All rights reserved.
25
Practical part
Use projections for save
operations.
Todo:
• Load Movie via
Neo4jTemplate the
Movie -> Actor
projection
• Edit the Movie
(release year) and call
save
• Ensure that all
relationships still exist
© 2023 Neo4j, Inc. All rights reserved.
..there is more, but just so little time
• Reactive support
• More query styles, like find by example
• Fluent Neo4jClient API
• …
© 2023 Neo4j, Inc. All rights reserved.
Stay connected
• GitHub project pages
◦ https://github.com/spring-projects/spring-data-neo4j
• Neo4j Community
◦ https://community.neo4j.com/c/drivers-stacks/spring-data-neo4j-ogm/30
• Developer Blog
◦ https://medium.com/neo4j
© 2023 Neo4j, Inc. All rights reserved.
© 2023 Neo4j, Inc. All rights reserved.
28
Thank you!

Neo4j Training Series - Spring Data Neo4j

  • 1.
    © 2023 Neo4j,Inc. All rights reserved. © 2023 Neo4j, Inc. All rights reserved. 1 Spring Data Neo4j Concepts & Application Development Gerrit Meier and Michael Simons, SDN and OGM team @ Neo4j
  • 2.
    © 2023 Neo4j,Inc. All rights reserved. Who are we? Michael Simons Gerrit Meier
  • 3.
    © 2023 Neo4j,Inc. All rights reserved. Who are you? • Java Developer • Graph Specialist • Where are you from? • …
  • 4.
    © 2023 Neo4j,Inc. All rights reserved. Today’s plan • Get your development environment set up (If not already done) • Create Neo4j Java Driver-based Spring Boot applications • Concepts and Architecture of Spring Data Neo4j • Advance to a Spring Data Neo4j application ◦ Modelling ◦ Repositories • Control what gets loaded • Custom queries and projections • Working with Neo4jTemplate • Use Neo4jTemplate features • Testing • Add tests to your application
  • 5.
    © 2023 Neo4j,Inc. All rights reserved. Development environment • (free) Aura instance running • JDK 17 ◦ Driver and Spring Boot • IDE • Allow access to download artifacts from Maven Central • Docker for testing
  • 6.
    © 2023 Neo4j,Inc. All rights reserved. Database preparation
  • 7.
    © 2023 Neo4j,Inc. All rights reserved. Spring Boot application • start.spring.io • no dependencies • Maven/Gradle choose ◦ Choose what you are comfortable with ◦ Will use Maven
  • 8.
    © 2023 Neo4j,Inc. All rights reserved. 8 Live coding / code along Project setup
  • 9.
    © 2023 Neo4j,Inc. All rights reserved. © 2023 Neo4j, Inc. All rights reserved. 9 Practical part Create a Spring Boot application using the Neo4j Java Driver. Todo: • Add Neo4j Java Driver • Configuration in application.properties • Implement CommandLineRunner • Output all Movie-node titles
  • 10.
    © 2023 Neo4j,Inc. All rights reserved. Spring Data Neo4j Architecture Neo4j Java Driver spring-boot-autoconfigure provides
  • 11.
    © 2023 Neo4j,Inc. All rights reserved. Spring Data Neo4j Architecture Neo4j Java Driver spring-boot-starter-data-neo4j spring-boot-autoconfigure provides provides Spring Data Neo4j
  • 12.
    © 2023 Neo4j,Inc. All rights reserved. Spring Data Neo4j Architecture Spring Data Repositories Neo4j Template Neo4j Client Neo4j Java Driver spring-boot-starter-data-neo4j spring-boot-autoconfigure provides provides
  • 13.
    © 2023 Neo4j,Inc. All rights reserved. Spring Data Neo4j Architecture Spring Data Repositories Neo4j Template Neo4j Client Neo4j Java Driver spring-boot-starter-data-neo4j spring-boot-autoconfigure provides provides
  • 14.
    © 2023 Neo4j,Inc. All rights reserved. @Node("Person") public class Actor { @Id public final String name; @Relationship("ACTED_IN") public final List<Movie> movies; public Actor(String name, List<Movie> movies) { this.name = name; this.movies = movies; } } @Node public class Movie { @Id public final String title; public Movie(String title) { this.title = title; } }
  • 15.
    © 2023 Neo4j,Inc. All rights reserved. 15 Live coding / code along Spring Data Neo4j (starter) dependency Model domain classes First repository
  • 16.
    © 2023 Neo4j,Inc. All rights reserved. © 2023 Neo4j, Inc. All rights reserved. 16 Practical part Enrich the application by a PersonRepository with additional functionality. Todo: • Create a repository for the Person • Return Tom Hanks with all Movies, he acted in.
  • 17.
    © 2023 Neo4j,Inc. All rights reserved. @Node public class Person { @Id public final String name; public Person(String name) { this.name = name; this.movies = movies; } } @Node public class Movie { @Id public final String title; @Relationship(type="ACTED_IN", direction= Relationship.Direction.INCOMING) public List<Person> actors; @Relationship(type="DIRECTED", direction= Relationship.Direction.INCOMING) public List<Person> directors; @Relationship(type="PRODUCED", direction= Relationship.Direction.INCOMING) public List<Person> producers; public Movie(String title) { this.title = title; } }
  • 18.
    © 2023 Neo4j,Inc. All rights reserved. More on repositories Custom queries • Form your fetch shape by hand
  • 19.
    © 2023 Neo4j,Inc. All rights reserved. More on repositories Projections • Let SDN form your fetch shape
  • 20.
    © 2023 Neo4j,Inc. All rights reserved. © 2023 Neo4j, Inc. All rights reserved. 20 Practical part Use existing functionality in Spring Data Neo4j to load only the data you are interested in. Todo: • Custom query: Find a Movie by title with just the actors attached • Projection: Find a Movie by title with just the actors (additional PersonProjection)
  • 21.
    © 2023 Neo4j,Inc. All rights reserved. Pitfalls of Sub-Graph loading • Loaded slice is incomplete ◦ SDN takes the object (graph) to persist as the only truth ◦ Relationships will get removed • Open projections (DTOs) will adapt entity metadata and fall back to original query creation ◦ Also in multi-level projections starting with interfaces • Use-case specific projections ◦ A lot of interfaces/classes for each use-case
  • 22.
    © 2023 Neo4j,Inc. All rights reserved. 22 Live coding / code along Neo4jTemplate with Projections
  • 23.
    © 2023 Neo4j,Inc. All rights reserved. © 2023 Neo4j, Inc. All rights reserved. 23 Practical part Use projections for save operations. Todo: • Load Movie via Neo4jTemplate the Movie -> Actor projection • Edit the Movie (release year) and call save • Ensure that all relationships still exist
  • 24.
    © 2023 Neo4j,Inc. All rights reserved. Testing • Testslice @DataNeo4jTest ◦ Set up transaction manager, repositories, etc. • Testcontainers Neo4j ◦ Docker based solution to run integration tests
  • 25.
    © 2023 Neo4j,Inc. All rights reserved. © 2023 Neo4j, Inc. All rights reserved. 25 Practical part Use projections for save operations. Todo: • Load Movie via Neo4jTemplate the Movie -> Actor projection • Edit the Movie (release year) and call save • Ensure that all relationships still exist
  • 26.
    © 2023 Neo4j,Inc. All rights reserved. ..there is more, but just so little time • Reactive support • More query styles, like find by example • Fluent Neo4jClient API • …
  • 27.
    © 2023 Neo4j,Inc. All rights reserved. Stay connected • GitHub project pages ◦ https://github.com/spring-projects/spring-data-neo4j • Neo4j Community ◦ https://community.neo4j.com/c/drivers-stacks/spring-data-neo4j-ogm/30 • Developer Blog ◦ https://medium.com/neo4j
  • 28.
    © 2023 Neo4j,Inc. All rights reserved. © 2023 Neo4j, Inc. All rights reserved. 28 Thank you!