You now know how to add a Movie
node to the database. But what if you also want to add the actors who acted in the movie? You can do this by passing a little more data to the save()
method.
Testing the save() method with relationships
You can include information for saving relationships to the JSON file and pass it to the save()
method without any additional code changes.
Because the actors
field is already part of the Movie
domain class, the extra data will automatically get mapped. The actors
field is a List<Role>
which traverses to the Role
class then to the Person
class via the relationship, so the JSON file needs to contain an array of Person
objects. The save()
method will then map those to the actors
field.
Just like in the last lesson to add a new movie, the movieRels.json
file below (src/main/resources
folder) shows the added data.
{
"movieId": "9876",
"title": "MyMovie",
"actors": [
{ "role": "self",
"person": {
"imdbId": "12",
"name": "Jennifer Reif"
}
}
]
}
Now run the application, then execute the statement below in the terminal to test.
curl -X POST 'localhost:8080/movies/save' \
-H 'Content-Type: application/json; charset=utf-8' \
-d @src/main/resources/movieRels.json
Output from the command should look something like this:
{"movieId":"9876","title":"MyMovie","plot":null,"poster":null,"url":null,"imdbId":null,"tmdbId":null,"released":null,"year":null,"runtime":null,"budget":null,"revenue":null,"imdbVotes":null,"imdbRating":null,"languages":null,"countries":null,"actors":[{"id":null,"role":"self","person":{"imdbId":"12","tmdbId":null,"name":"Jennifer Reif","bio":null,"poster":null,"url":null,"bornIn":null,"born":null,"died":null}}]}
And check the results in the database by running the query populated in the right-hand tab.
Just as before, feel free to play around with the data in the JSON file and save different variations of the movie and actors.
Lesson Summary
In this lesson, you learned how to save nodes with their relationships to the database.
Next, you will learn how to delete nodes in the database.