Ok so I am currently working on a project which is an IMDB replica and I have an User that can review a Movie. User and Movie are 2 nodes and :REVIEWED is a relationship from User to Movie that contains the content of the review.
This is the User class
@Node("USER")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@GeneratedValue
private Long id;
@AgeAnnotaion
private Integer age;
@Email(message = "Wrong email format")
private String email;
@NotBlank(message = "The User's name cannot be null or empty")
private String name;
@NotBlank(message = "The User's password cannot be null or empty")
private String password; // TODO: create an annotation that verifies password for more details
@Relationship(type = "REVIEWED", direction = Relationship.Direction.OUTGOING)
private Collection<Reviewed> reviewedMovies;
@Relationship(type = "IN_WATCHLIST", direction = Relationship.Direction.OUTGOING)
private Collection<Movie> watchlistMovies;
}
This is the Movie class
@Node("MOVIE")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Movie {
@Id
@GeneratedValue
private Long id;
@NotBlank(message = "Movie's name cannot be null or blank")
private String name;
@NotBlank(message = "Movie's genre cannot be null or blank")
private String genre;
@ReleaseYearAnnotation
private Integer releaseYear;
@Relationship(type = "DIRECTED", direction = Relationship.Direction.INCOMING)
private Director director;
@Relationship(type = "ACTED_IN", direction = Relationship.Direction.INCOMING)
private Collection<Actor> actedInActors;
@Relationship(type = "IN_WATCHLIST", direction = Relationship.Direction.INCOMING)
private Collection<User> watchListUsers;
@Relationship(type = "REVIEWED", direction = Relationship.Direction.INCOMING)
private Reviewed reviewer;
}
And here is the :REVIEWED relationship class
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@RelationshipEntity(type = "REVIEWED")
public class Reviewed {
@RelationshipId private Long id;
@StartNode private User user;
@EndNode private Movie movie;
@Property private String content;
}
I also added a ReviewController and ReviewRepository to test it
@RestController
@RequestMapping("/api/review")
public class ReviewController {
private final ReviewRepository reviewRepository;
public ReviewController(ReviewRepository reviewRepository) {
this.reviewRepository = reviewRepository;
}
@GetMapping
public String getReview(@RequestParam("movie") String movieName) {
System.out.println("Movie name is " + movieName);
Reviewed reviewed = this.reviewRepository.getReview();
return reviewed.getContent();
}
}
@Repository
public interface ReviewRepository extends Neo4jRepository<Reviewed, Long> {
@Query("MATCH (u:USER)-[r:REVIEWED]->(m:MOVIE) RETURN r")
Reviewed getReview();
}
I searched everywhere on how to map the relationship and I saw that this is the way to do it. Can someone help me please?
P.S.: The url is http://localhost:8080/api/review?movie=Venom
I also have the following dependecies in my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.movie</groupId>
<artifactId>MovieSite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MovieSite</name>
<description>MovieSite</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- VALIDATION -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- LOMBOK -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!-- JSON -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20211205</version>
</dependency>
<!-- NEO4J -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-core</artifactId>
<version>3.2.31</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-bolt-driver</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
</dependency>
<!-- SPRING WEB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DEVTOOLS -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- SPRING BOOT TEST -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>