JasonShuyinta / GraphDB-Neo4j

A basic project to setup a Neo4j DB with Spring

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

docker pull neo4j

After that, start the container with:

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):

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-neo4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.26</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

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:

@Relationship(type = "TEAMMATE")
    public Set<Person> teammates;

    @Relationship(type = "OWNS")
    public Set<Dog> 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:

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<Person> 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.

About

A basic project to setup a Neo4j DB with Spring


Languages

Language:Java 100.0%