spring-boot - 如何通过 Postman 修复 Spring Boot(一对多、一对一)中的更新过程?
问题描述
我有关于更新电影的问题。
我在 MovieService 中编写了一个以“更新”命名的函数。
这是如下所示的功能。
public void update(Long id,Movie movie) {
boolean isUpdatingEmployee = (movie.getId() == id);
if (isUpdatingEmployee) {
Movie existingMovie = movieRepository.findById(movie.getId()).get();
existingMovie.setId(id);
existingMovie.setName(movie.getName());
existingMovie.setRating(movie.getRating());
existingMovie.setDirector(movie.getDirector());
existingMovie.setGenres(movie.getGenres());
existingMovie.setCreatedAt(movie.getCreatedAt());
movieRepository.save(existingMovie);
}
}
当我在保存后尝试更新电影时,我得到了这种 JSON 结果,这就是无法完成更新过程的原因。
http://localhost:8082/api/v1/movie/update/1
正文请求
{
"name": "MovieC",
"genres": [
{
"name" : "Adventure"
},
{
"name" : "Action"
}
],
"createdAt": "2021-04-28",
"rating" : 9,
"director" : {
"name" : "Director 2"
}
}
更新流程后的 JSON 结果。
{
"id": null,
"name": "MovieC",
"genres": [
{
"id": null,
"name": "Action"
},
{
"id": null,
"name": "Adventure"
}
],
"rating": 9.0,
"createdAt": "2021-04-28",
"director": {
"id": null,
"name": "Director 2"
}
}
这是我的电影实体,如下所示。
@Entity
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Movie implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@JsonManagedReference
@OneToMany(mappedBy="movie",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Genre> genres;
private Double rating;
private LocalDate createdAt;
@ManyToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn
private Director director;
}
这是我的 Director 实体,如下所示。
@Entity
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties({"movies"})
public class Director implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NonNull
private String name;
@OneToMany(mappedBy="director",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Movie> movies;
}
这是我的流派实体,如下所示。
@Entity
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties({"movie"})
public class Genre implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NonNull
private String name;
@JsonBackReference
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn
private Movie movie;
}
这是我的示例项目链接:项目链接
我该如何解决?
解决方案
根据您的代码,这是您的要求:
http://localhost:8082/api/v1/movie/update/1
{
"name": "MovieC",
"genres": [
{
"name" : "Adventure"
},
{
"name" : "Action"
}
],
"createdAt": "2021-04-28",
"rating" : 9,
"director" : {
"name" : "Director 2"
}
}
现在考虑您的代码中的这个片段:
public void update(Long id,Movie movie) {
boolean isUpdatingEmployee = (movie.getId() == id);
if (isUpdatingEmployee) {
...
您id
将1
在路径变量中设置它。但是,movie.getId()
它将为空,因为我在您的RequestBody
.
所以:
isUpdatingEmployee = (movie.getId() == id)`
isUpdatingEmployee = ( null == 1)
isUpdatingEmployee = false
这将永远给你false
,所以我认为这不会进入你的更新逻辑。
推荐阅读
- r - 将参数从自定义函数传递给 group_by 不起作用
- r - logisitc/probit 回归中因变量的比例/拆分
- javascript - 如何更改每个特定页面的布局元素样式?
- c# - 在 asp.net 核心中更新身份用户时并发失败
- flutter - 给容器边界颤动空间
- amazon-ec2 - 如何在 EC2 实例上的 docker 内使用 AWS sbatch (SLURM)?
- java - 编译时出现 IntelliJ Idea 问题 - 找不到对象映射器类
- firebase - Flutter Streambuilder 返回循环
- c++ - 从函数返回时的原始/对象值的生命周期
- python - matplotlib - 如何将次要网格线的数量设置为 6 而不是 4?