...

/

Neo4j Nodes and Repositories

Neo4j Nodes and Repositories

Learn about nodes and repositories through the Spring Data Neo4j framework.

In Neo4j, a graph node is a data entity with a unique identifier representing real-world entities in the graph database with properties and connections to other nodes through relationships.

Graph nodes

Let’s look at a familiar example: the Book and Author POJOs referencing the book and author database graph nodes and convert them into Neo4j node classes.

The Book node

Let’s learn a few handy annotations to transform a POJO into a Neo4j graph node.

The most common annotations are @Node and @RelationshipId.

  • The @Node annotation allows mapping a POJO to a database node by the underlying data access layer.

  • The @Id annotation allows mapping a property to an identifier in the database node.

We use these annotations to convert the Book POJO from the com.smartdiscover.model package to a Neo4j graph node and map it to the book node:

Press + to interact
package com.smartdiscover.model;
import lombok.Data;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.support.UUIDStringGenerator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Data
@Node("book")
public class Book {
@Id
@GeneratedValue(UUIDStringGenerator.class)
private String id;
private String name;
private String summary;
private List<Author> authors;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", summary='" + summary + '\'' +
((null != authors && !authors.isEmpty()) ? ", authors=" + authors.stream().map(i -> i.getFullName()).collect(Collectors.toList()) + '\'' : "") +
'}';
}
}

Code explanation:

  • Line 17: We utilize Lombok’s @Data ...