https://github.com/jazzshu/graphdb-neo4j
A basic project to setup a Neo4j DB with Spring
https://github.com/jazzshu/graphdb-neo4j
graphdb java neo4j spring
Last synced: 9 months ago
JSON representation
A basic project to setup a Neo4j DB with Spring
- Host: GitHub
- URL: https://github.com/jazzshu/graphdb-neo4j
- Owner: jazzshu
- Created: 2023-02-13T21:49:59.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-13T22:10:18.000Z (about 3 years ago)
- Last Synced: 2025-02-24T09:32:10.339Z (about 1 year ago)
- Topics: graphdb, java, neo4j, spring
- Language: Java
- Homepage:
- Size: 120 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Spring - Neo4J
This is a basic implementation of a Graph DB, in particular Neo4j with the Spring framework.
First of all you need to download and install Neo4j on your local machine, or simple pull the Docker image as I did with the following command:
```shell
docker pull neo4j
```
After that, start the container with:
```shell
docker run --publish=7474:7474 --publish=7687:7687 --volume=$HOME/neo4j/data:/data neo4j
```
One the container is up we can start building our project.
Using Spring Initializer you must choose Java 17 in order for Neo4j to work,
then as the dependencies choose the following ones for Gradle or Maven (**spring-boot-starter-data-neo4j** and **lombok**):
```xml
org.springframework.boot
spring-boot-starter-data-neo4j
org.projectlombok
lombok
1.18.26
provided
```
After you updated and built your project you can start creating your entities.
### Entities
I created 3 entities: Person, Dog and Toy.
Person has a single property (obviously without considering the Id), which a String for its name.
Dog has a single property as well for its race and Toy has 2 properties which are price and name.
Each of these entities need to be annotated with @Node.
The id's need to be annotated with @Id and @GeneratedValue. These 3 annotations are imported from **import org.springframework.data.neo4j.core.schema**.
### Relationships
The Person entity has 2 relationships: TEAMMATE and OWNS.
A Person can have one or more Teammates (which is another Person entity), and can own a Dog.
To create a relationship this is the syntax:
```java
@Relationship(type = "TEAMMATE")
public Set teammates;
@Relationship(type = "OWNS")
public Set dogs;
public void worksWith(Person person) {
if(teammates == null) {
teammates = new HashSet<>();
}
teammates.add(person);
}
public void owns(Dog dog) {
if(dogs == null) {
dogs = new HashSet<>();
}
dogs.add(dog);
}
```
As you can see, teammates and dogs are both a Set as they are sort of a "One-To-Many" relationship in relational databases.
A Dog can have a relationship of type "PLAYS_WITH" with a Toy Entity, while Toy has no entity with anyone.
With the methods we defined it is fairly simple to implements relationships between entities as we can see in our main method:
```java
Person greg = new Person("Greg");
Person roy = new Person("Roy");
Person craig = new Person("Craig");
Dog labrador = new Dog("Labrador");
Dog pitbull = new Dog("Pitbull");
Dog chihuaha = new Dog("Chihuaha");
Dog amstaff = new Dog("Amstaff");
Toy ball = new Toy(12.3, "Ball");
Toy chain = new Toy(2.3, "Chain");
Toy doll = new Toy(1.4, "Doll");
List team = Arrays.asList(greg, roy, craig);
log.info("Before linking up to Neo4j..");
personRepository.save(greg);
personRepository.save(roy);
personRepository.save(craig);
greg = personRepository.findByName(greg.getName());
greg.worksWith(roy);
greg.owns(labrador);
greg.owns(pitbull);
labrador.playsWith(ball);
labrador.playsWith(chain);
pitbull.playsWith(chain);
greg.worksWith(craig);
craig.owns(chihuaha);
chihuaha.playsWith(chain);
craig.owns(labrador);
chihuaha.playsWith(doll);
roy.owns(amstaff);
personRepository.save(greg);
```
Here we can see that we created various relationships between entities and no joins were needed.
Remember to annotate your main class with @EnableNeo4jRepositories.