Neo4j :

I am Hossein Koraei and, in this article, I am going to briefly discuss how to run Neo4j and use GraphQl. If you are unfamiliar with what I said, I suggest that you read the Neo4j and GraphQl articles before continuing reading this article.

Introduction Neo4j

We use the Java and the Spring framework for running in this article. You can go to the Neo4j website, for getting instructions for downloading, installing, and configuring Neo4j.  We also used Neo4j Browser software, which is used to simplify working with data. You can download the versions that run in your system from the link.

Example Description

In this example, we want to show what cars each car company has produced and what sponsorship each car has.  Each sponsor can also own a car separately.

Step 1

First of all, we add the required dependencies to our project. 

  spring-boot-starter-data-neo4j
  neo4j-ogm-test
  neo4j-ogm-embedded-driver
  neo4j-java-driver-spring-boot-starter

Step 2

We should choose our classes and use @Node to consider them as Node.

Company Class:

@Node
public class Company {

@Id
    private long id;
    private String name;

@Relationship(type = "OWN_IN", direction = Relationship.Direction.OUTGOING)
    private List<Car> carList;

public Company(long id, String name, List<Car> carList) {
        this.id = id;
        this.name = name;
        this.carList = carList;
    }

public long getId() {
        return id;
    }

public String getName() {
        return name;
    }

public List<Car> getCarList() {
        return carList;
    }
}</Car></Car></Car>

Car Class:

@Node
public class Car {

@Id
    private long id;
    private String name;
    private int year;

@Relationship(type = "OWN_ENGINE", direction = Relationship.Direction.OUTGOING)
    private List<Engine> engines;

public Car(long id, String name, int year, List<Engine> engines) {
        this.id = id;
        this.name = name;
        this.year = year;
        this.engines = engines;
    }

public long getId() {
        return id;
    }

public String getName() {
        return name;
    }

public int getYear() {
        return year;
    }

public List<Engine> getEngines() {
        return engines;
    }
}</Engine></Engine></Engine>

And sponsor class:

@Node
public class Sponsor {

@Id
    private long id;
    private String name;

@Relationship(type = "OWN_CAR", direction = Relationship.Direction.OUTGOING)
    private List<Car> carList;

public Sponsor(long id, String name, List<Car> carList) {
        this.id = id;
        this.name = name;
        this.carList = carList;
    }

public long getId() {
        return id;
    }

public String getName() {
        return name;
    }

public List<Car> getCarList() {
        return carList;
    }
}</Car></Car></Car>

Step 3

In this step, we try to apply CRUD with inner program data.  

@Repository
public interface CarRepository extends Neo4jRepository<Car, string=""> {
    
Car findByName(String name);

@Query("MATCH(n:Car START WITH {$pattern}) RETURN n")
    List<Car> findByPatternName(@Param("pattern") String pattern);
}</Car></Car,>

As you can see, for finding a node or a list of nodes you can use two methods. The first method is to use the feature provided by Neo4j. You can enter the function name, findByName finds the node target by entering its name. If two nodes have the same name, it returns the first node found. The second method is to use @Query. This way is more accurate and complete. You can find the node you want. In the next detailed article, we will talk about queries in Neo4j.

For running other operations, such as adding and removing nodes, you can use the save and delete functions in the service layer. To understand better, please see the code snippet below.

@Service
public class CarService {
    
@Autowired
    private CarRepository carRepository;
    
private void add(
            long id,
            String name,
            int year,
            List<Sponsor> sponsors
    ){
        Car car = new Car(
                id,
                name,
                year,
                sponsors
        );
        
carRepository.save(car);
    }

private void delete(String id){

carRepository.deleteById(id);
    }
}</Sponsor>

You can download the relevant project from the GitHub link.

And in the end,

Mirbazorgi’s website aims to help you learn and fix your problems by providing articles and practical experiences. Email me if you have any questions.

Leave A Comment

− 1 = 2

Please Send Email

Your message sent successfully
There has been an error