jpa - Spring - ManyToMany foreign key error on delete -
i've checked lot of questions manytomany issues on "delete" action, didn't find matching error.
i'm managing list of movies ; each movie can have 0 n actors , 0 or 1 director, each actor can play in 1 n movies, , director can produce 1 n movies. i've created entities, , want movie managing entity. i'm using crudrepository , i'm working rest services. entities , repository/rest controller below.
i have 2 errors following code:
- if comment onetomany , manytomany parts in director , actor, can add/update/get movies, can't delete them. looks app trying delete actors well, , fails if actor referenced multiple times in movie_actor table (i have psqlexception saying can't use delete on table actor because of foreign key constraint of movie_actor table)
- if uncomment onetomany , manytomany parts, can add movies, that's all. if try movies, looks there loop or because server crashes , have response 100x list of movies (all of them, multiple times...). on delete action, have issue explained in previous point.
movie
// ... imports ... @entity public class movie { @id @generatedvalue(strategy = generationtype.identity) @column(name = "mov_id") private long id; // ... other properties ... @manytoone @joincolumn(name = "dir_id") private director director; @manytomany(cascade = cascadetype.all) @jointable(name = "movie_actor", joincolumns = @joincolumn(name = "mov_id"), inversejoincolumns = @joincolumn(name = "act_id")) private list<actor> actors; // ... constructors ... // ... getters , setters... public director getdirector() { return director; } public void setdirector(director director) { this.director = director; } public list<actor> getactors() { return actors; } public void setactors(list<actor> actors) { this.actors = actors; } }
actor
// ... imports ... @entity public class actor { @id @generatedvalue(strategy = generationtype.identity) @column(name = "act_id") private long id; // ... other properties ... // @manytomany(mappedby = "actors") // private list<movie> movies; // ... constructors ... // ... getters , setters ... // public list<movie> getmovies() { // return movies; // } // // public void setmovies(list<movie> movies) { // this.movies = movies; // } }
director
// ... imports ... @entity public class director { @id @generatedvalue(strategy = generationtype.identity) @column(name = "dir_id") private long id; // ... other properties ... // @onetomany(mappedby = "director", fetch = fetchtype.lazy) // private list<movie> movies; // ... constructors ... // ... getters , setters ... // public list<movie> getmovies() { // return movies; // } // // public void setmovies(list<movie> movies) { // this.movies = movies; // } }
** movierepository **
// ... imports ... @repository public interface movierepository extends crudrepository<movie, long> { public list<movie> findall(); }
** moviecontroller **
// ... imports ... @restcontroller @requestmapping(value="/resources/movies") public class moviecontroller { @autowired movierepository movierepository; @autowired movieservice movieservice; @requestmapping(method = requestmethod.get) public responseentity<list<movie>> getall(){ return new responseentity<list<movie>>( movierepository.findall(), httpstatus.ok); } @requestmapping(method = requestmethod.post) public responseentity<movie> add(@requestbody moviedto dto) throws exception { movie movie = movieservice.addmovie(dto); return new responseentity<movie>( movie, httpstatus.created); } @requestmapping(value="/{id}", method = requestmethod.get) public responseentity<movie> getone(@pathvariable long id){ return new responseentity<movie>( movierepository.findone(id), httpstatus.ok) ; } @requestmapping(value="/{id}", method = requestmethod.put) public responseentity<movie> update(@pathvariable long id, @requestbody moviedto dto) throws exception { dto.setmovieid(id); return new responseentity<movie>( movieservice.updatemovie(dto), httpstatus.ok); } @requestmapping(value="/{id}", method = requestmethod.delete) public httpstatus delete(@pathvariable long id) throws exception { movierepository.delete(id); return httpstatus.no_content; } }
thanks help, i'm stuck , don't see i'm doing wrong compared tutorials...
Comments
Post a Comment