Last lesson, you mapped the ACTED_IN
relationship from the graph to the actors
field in the Movie
class. Now you will map the relationship’s property role
by creating a separate Role
class to contain the field.
Add relationship properties class
-
Create a new class called
Role.java
in thesrc/main/java/com/example/appspringdata
package. -
Import
RelationshipId
,RelationshipProperties
, andTargetNode
classes.javaimport org.springframework.data.neo4j.core.schema.RelationshipId; import org.springframework.data.neo4j.core.schema.RelationshipProperties; import org.springframework.data.neo4j.core.schema.TargetNode;
-
Just above the class definition, add the
@RelationshipProperties
annotation.java@RelationshipProperties
-
Add the properties to the class, including an
id
field (annotated with@RelationshipId
), therole
field, and aperson
field (annotated with@TargetNode
) to specify the end node of the relationship.java@RelationshipId private String id; private String role; @TargetNode private final Person person;
-
Add a class constructor, getter methods for all fields, and a setter method for the
role
field.javapublic Role(String id, String role, Person person) { this.id = id; this.role = role; this.person = person; } public String getId() { return id; } public String getRole() { return role; } public Person getPerson() { return person; }
Role setter methods
You do not need setter methods for the id
and person
fields because the id is handled by the database, and the Person entity is handled by a separate class.
Completed code is available below to check your work.
Click to reveal the completed Role
class code
import org.springframework.data.neo4j.core.schema.RelationshipId;
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
import org.springframework.data.neo4j.core.schema.TargetNode;
@RelationshipProperties
public class Role {
@RelationshipId
private String id;
private String role;
@TargetNode
private final Person person;
public Role(String id, String role, Person person) {
this.id = id;
this.role = role;
this.person = person;
}
public String getId() {
return id;
}
public String getRole() {
return role;
}
public Person getPerson() {
return person;
}
}
Update Movie class
Next, you need to update the Movie.java
class to reference the new relationship class Role
, rather than Person
.
Update the actors
variable to return a List<Role>
, then update the related getter and setter for the same change.
Completed code is available below to check your work.
Click to reveal the updated Movie
class code
// tag::movie-roles[]
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
import java.util.List;
@Node
public class Movie {
@Id
private String movieId;
private String title;
private String plot;
private String poster;
private String url;
private String imdbId;
private String tmdbId;
private String released;
private Long year;
private Long runtime;
private Long budget;
private Long revenue;
private Long imdbVotes;
private Double imdbRating;
private String[] languages;
private String[] countries;
@Relationship(value = "ACTED_IN", direction = Relationship.Direction.INCOMING)
private List<Role> actors;
public Movie(String movieId, String title, String plot, String poster, String url, String imdbId, String tmdbId,
String released, Long year, Long runtime, Long budget, Long revenue, Long imdbVotes, Double imdbRating,
String[] languages, String[] countries, List<Role> actors) {
this.movieId = movieId;
this.title = title;
this.plot = plot;
this.poster = poster;
this.url = url;
this.imdbId = imdbId;
this.tmdbId = tmdbId;
this.released = released;
this.year = year;
this.runtime = runtime;
this.budget = budget;
this.revenue = revenue;
this.imdbVotes = imdbVotes;
this.imdbRating = imdbRating;
this.languages = languages;
this.countries = countries;
this.actors = actors;
}
public String getMovieId() {
return movieId;
}
public String getTitle() {
return title;
}
public String getPlot() {
return plot;
}
public String getPoster() {
return poster;
}
public String getUrl() {
return url;
}
public String getImdbId() {
return imdbId;
}
public String getTmdbId() {
return tmdbId;
}
public String getReleased() {
return released;
}
public Long getYear() {
return year;
}
public Long getRuntime() {
return runtime;
}
public Long getBudget() {
return budget;
}
public Long getRevenue() {
return revenue;
}
public Long getImdbVotes() {
return imdbVotes;
}
public Double getImdbRating() {
return imdbRating;
}
public String[] getLanguages() {
return languages;
}
public String[] getCountries() {
return countries;
}
public List<Role> getActors() {
return actors;
}
}
// end::movie-roles[]
Test the application (again!)
Start the application and run curl 'localhost:8080/movies/2'
from the terminal tab. You should see the same list of movie properties as before, plus one for role
containing the name(s) of the character(s) the person played in that movie.
Nice work! You mapped the ACTED_IN
relationship from the graph to the actors
field in the Movie
class, and you mapped a relationship property for role
by creating a separate Role
class to contain the field.
Summary
In this lesson, you mapped the relationship property for the roles actors played in movies by creating a separate Role
class.
Next, you can optionally learn about some application modeling mistakes to avoid when working with Neo4j. Or you can continue onto the next module to learn how to write data to the database from the application.