Java – spring data break uses the manytomany relationship to post a new project
I have two entities: actors and movies There is a manytomany relationship between the two (because and actors can connect multiple movies, and you can see multiple actors in movies) On my spring data rest API, I have the following endpoints:
http://host:port/movies http://host:port/actors
Now suppose I'm going to create a new actor from the movie page My client will submit a (single) post request containing actor information and relationship with the movie I tried something like the following (new actors in the movie with ID 1):
{ "name": "Leonardo Di Caprio","movies": [ "http://host:port/movies/1" ] }
The spring API replies 201 created, so the format and movie URI are very good When I query API or dB for actor, I find that actor has been created, but the relationship does not exist
I already know that you should make two requests for the relationship between spring Tammany and spring data rest (one for creating actors and one for creating relationships) Here I ask if there is a way to create both with a single request (such as onetomany / manytoone or onetoone relationship)
Actor class
@Entity public class Actor { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String name; @ManyToMany(mappedBy = "actors") private List<Movie> movies; public long getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Movie> getMovies() { return movies; } public void setMovies(List<Movie> movies) { this.movies = movies; } }
Film class
@Entity public class Movie { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) protected long id; protected String title; @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH}) protected List<Actor> actors; public long getId() { return id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<Actor> getActors() { return actors; } public void setActors(List<Actor> actors) { this.actors = actors; } }
For these two entities, I have a standard Repository:
@Repository public interface ActorRepository extends PagingAndSortingRepository<Actor,Long> { }
UPDATE
The behavior I face is due to how JPA handles the manytomany relationship In this thread, there is a clever explanation of how to deal with the bidirectional association between JPA and rest
I can solve my problem with one of the following two options:
A – execute two post requests, one open
http://host:port/actors
Insist on a new actor and a person
http://host:port/movies/{id}/actors
As follows:
... | Content-Type: text/uri-list | headers ... | http://host:port/actors/{id-of-the-new-actor} | body
Stick to the connection between new actors and films
B – execute only one post request
http://host:port/actors
(as I described at the beginning of the question) but changed the setmovies method in the actor class (as described in the topic I referenced)
Solution
First create the resource:
curl -i -X POST -H "Content-Type:application/json" -d "{\"name\":\"Leonardo Di Caprio\"}" http://host:port/actors
Then make a movie:
curl -i -X POST -H "Content-Type:application/json" -d "{\"title\":\"Titanic\"}" http://host:port/movies
Finally create an association (assuming http://host:port/actors/1 Is DiCaprio URI):
curl -i -X PUT -H "Content-Type:text/uri-list" --data-binary @movies.txt http://host:port/actors/1/movies
Movies that contain the URIs of the movie Txt, each on a separate line:
http://host:por/movies/1 http://host:por/movies/2
Follow this useful @ L_ 301_ 3@