The movie data model shows entities for movies, people, and the relationships between them. You will start your application domain with the Movie entity.
Movie domain class
A Java domain class will represent the Movie entity. While you wouldn’t need to map all of the fields on the Movie
node, you will go ahead and map all of them for this example.
Open the Movie.java
class in the src/main/java/com/example/appspringdata
directory.
Click to reveal completed Movie
domain class code
package com.example.appspringdata;
import org.springframework.data.annotation.Id;
import org.springframework.data.neo4j.core.schema.Node;
@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;
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) {
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;
}
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;
}
}
Review the code and note the following:
-
The
Movie
class definition:java@Node public class Movie {
The
@Node
annotation above the class definition marks the class as a node entity. -
That
Movie
node properties are mapped toMovie
class attributes with a relevant data type:textMovie node properties and data types{ budget:"INTEGER", movieId:"STRING", tmdbId:"STRING", imdbVotes:"INTEGER", runtime:"INTEGER", countries:"LIST OF STRING", imdbId:"STRING", url:"STRING", plot:"STRING", released:"STRING", languages:"LIST OF STRING", imdbRating:"FLOAT", title:"STRING", poster:"STRING", year:"INTEGER", revenue:"INTEGER" }
javaMovie class attributes@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;
The
@Id
annotation on themovieId
field marks it as the node identifier. -
The class constructor:
javapublic 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) { 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; }
-
Getter and setters for each property:
public String getMovieId() {
return movieId;
}
public String getTitle() {
return title;
}
...
MovieId
For simplicity, you are using the existing movieId
key value and only the @Id
annotation.
The preferred method is to use a UUIDGenerator or optimistic locking with external id values.
Related entity in constructor
You can include the related entity in the constructor because there isn’t a bidirectional relationship defined (Movie←Person, Person→Movie) and the data model is not cyclic (you cannot iterate over same movie and person multiple times).
Advice on how to manage bidirectional relationships is covered later in this module.
Summary
In this lesson, you created the Movie domain class.
Next, you will complete a challenge to apply the skills you learned here to create the Person domain class.