首页 > 解决方案 > 如何通过 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;
}

这是我的示例项目链接:项目链接

我该如何解决?

标签: spring-bootpostmancrud

解决方案


根据您的代码,这是您的要求:

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) {
    ...

id1在路径变量中设置它。但是,movie.getId()它将为空,因为我在您的RequestBody.

所以:

isUpdatingEmployee = (movie.getId() == id)` 
isUpdatingEmployee = (    null      ==  1)
isUpdatingEmployee = false

这将永远给你false,所以我认为这不会进入你的更新逻辑。


推荐阅读