当我更新属性并在 spring 启动时发送更新的请求时,为什么我没有更新响应?

Why don't I get the response updated when I update the properties and send an updated request in spring boot?

我正在使用 Angular、SpringBoot 和 PostgreSQL 开发一个 CRUD 应用程序。我在数据库中有两个名为“技能”和“员工”的 table。通过连接“技能”和“员工”table 创建的 table 是“employee_has”table。它们使用多对多关系进行映射。一个员工可以有很多技能。一项技能可以有很多员工。

在这里,当我创建具有技能的员工时,它正在正确创建。但是当我更新它并在前端提交后,技能不会得到更新。但是有效负载正在发生我所做的更改。回复只有之前的技能列表。

这是我的请求负载。

{id: 12, name: "Testing", dob: "2021-04-08T18:30:00.000+00:00", email: "testing@abc.com",…}
dob: "2021-04-08T18:30:00.000+00:00"
email: "testing@abc.com"
id: 12
name: "Testing"
skills: ["SQL", "C", "C++"]
0: "SQL"
1: "C"
2: "C++"

这是我的回复。

{id: 12, name: "Testing", dob: "2021-04-08T18:30:00.000+00:00", email: "testing@abc.com", skills: Array(2)}
dob: "2021-04-08T18:30:00.000+00:00"
email: "testing@abc.com"
id: 12
name: "Testing"
skills: Array(2)
0: {id: 9, skill_name: "C"}
1: {id: 10, skill_name: "C++"}
length: 2
__proto__: Array(0)
__proto__: Object

这就是我的 PUT 方法的样子。

@PutMapping("/employees/{id}")
    public ResponseEntity<Employee> updateEmployee(@PathVariable(value = "id") Long id,
                                                   @RequestBody @NotNull Employee employeeDetails) throws ResourceNotFoundException {
        Employee employee = employeeRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Employee not found for the id : " + id));

        employee.setName(employeeDetails.getName());
        employee.setDob(employeeDetails.getDob());
        employee.setEmail(employeeDetails.getEmail());

        List<Skill> skillListAll = skillRepository.findAll(); //list of all skills
        List<Skill> skillListEmployee = employee.getSkills();
        List<Skill> temp = new ArrayList<>();

        for(Skill s : skillListEmployee){
            for(Skill skillFromAll: skillListAll){
                if(s.getSkill_name().equals(skillFromAll.getSkill_name())){
                    temp.add(skillFromAll);
                }
            }
        }
        employee.setSkills(temp);
        Employee updatedEmployee = employeeRepository.save(employee);
        return ResponseEntity.ok(updatedEmployee);
    }

请帮我解决这个问题。

这是我的员工实体。

@Entity
@Table(name = "employees")

public class Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @NotNull
    @Column(name = "name")
    private String name;

    @Column(name = "dob")
    private Date dob;

    @NotNull
    @Column(name = "email", unique=true)
    private String email;

    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                    CascadeType.PERSIST,
                    CascadeType.MERGE
            })
    @JoinTable(name = "employee_has",
            joinColumns = { @JoinColumn(name = "emp_id") },
            inverseJoinColumns = { @JoinColumn(name = "skill_id") })
    private List<Skill> skills = new ArrayList<>();

    public Employee() {

    }

    public Employee(String name, Date dob, String email) {
        this.name = name;
        this.dob = dob;
        this.email = email;
    }

    public Employee(String name, Date dob, String email, List<Skill> skills) {
        this.name = name;
        this.dob = dob;
        this.email = email;
        this.skills = skills;
    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<Skill> getSkills() {
        return skills;
    }

    public void setSkills(List<Skill> skills) {
            this.skills = skills;
    }

}

你的 forloop 坏了

    List<Skill> skillListEmployee = employee.getSkills();
    for(Skill s : skillListEmployee){
        for(Skill skillFromAll: skillListAll){
            if(s.getSkill_name().equals(skillFromAll.getSkill_name())){
                temp.add(skillFromAll);
            }
        }
    }

在这里,您将只坚持员工已经具备的技能。

您可能想从 employeeDetails 而不是 employee

获得技能