我在用springboot训练,h2 hibernate,试着做一个完整的crud。一切正常,只是当我修改一个数据时,它会修改所选的数据,但会在我的列表中再添加一个数据。谁能告诉我我的代码出了什么问题?提前谢谢。
控制器:
@PutMapping("/updateMovie/{id}")
public ResponseEntity updateMovie(@PathVariable Long id, @RequestBody Movie movie){
return ResponseEntity.ok(movieService.updateMovie(id, movie));
}
服务
public Movie updateMovie(@PathVariable Long id, @RequestBody Movie movie){
Movie currentMovie = movieRepository.findById(id).orElseThrow(RuntimeException::new);
currentMovie.setTitle(movie.getTitle());
currentMovie.setDescription(movie.getDescription());
currentMovie.setDuration(movie.getDuration());
return movieRepository.save(movie);
}
模型
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name= "title")
private String title;
@NonNull
@Column(name= "duration")
private Integer duration;
@Column(name= "description")
private String description;
}