1

In my program I tried to update the user's email. But it gave me an exception saying

nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of java.util.HashSet

But when I update a user with adding skills and email it didn't gave me this exception.

I tried to create a default constructor but it didn't work. I also tried to use Json creator.It also didn't worked for me.I can see that the problem is when I pass the skill set empty this exception is rising.But I don't have a clear idea, how to over come this situation.

This is my Employee entity

@Entity
@Table(name = "Employee")
public class Employee implements Serializable{

    private static final long serialVersionUID = -3009157732242241606L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long emp_id;

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

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

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

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

    @ManyToMany(cascade = CascadeType.MERGE)
    @JoinTable(name = "emp_skills",
            joinColumns = @JoinColumn(name = "emp_id", referencedColumnName = "emp_id"),
            inverseJoinColumns = @JoinColumn(name = "s_id",referencedColumnName = "s_id"))
    private Set<Skills> skills;

    protected Employee(){

    }


    public Employee(String emp_fname,String emp_lname,String emp_email,Date emp_dob){
        this.emp_fname = emp_fname;
        this.emp_lname = emp_lname;
        this.emp_email = emp_email;
        this.emp_dob = emp_dob;
    }

    public Employee(Long emp_id,String emp_fname,String emp_lname,String emp_email,Date emp_dob,Set<Skills> skills){
        this.emp_id = emp_id;
        this.emp_fname = emp_fname;
        this.emp_lname = emp_lname;
        this.emp_email = emp_email;
        this.emp_dob = emp_dob;
        this.skills = skills;
    }

//all the getters and setter

This is my Skills entity

@Entity
@Table(name = "Skills")
public class Skills implements Serializable {

    private static final long serialVersionUID = -3009157732242241606L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long s_id;

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


    @ManyToMany(mappedBy = "skills",cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<Employee> employees;

    protected Skills(){

    }

    public Skills(String s_name){
        this.s_name = s_name;
    }

//all the getters and setter

This is my controller class method to update user

@Autowired
    EmployeeRepository repository;

    @RequestMapping("/updateEmployee")
    @PostMapping("/updateEmployee")
    @CrossOrigin
    public void updateEmployee(@RequestBody Employee employee, BindingResult bindingResult){
        Employee empToBeUpdated = null;
        for (Employee emp : repository.findAll()){
            if (emp.getEmp_id()==employee.getEmp_id()){
                empToBeUpdated = emp;
            }
        }
        if (!employee.getEmp_fname().equals("")&&employee.getEmp_fname()!=null){
            empToBeUpdated.setEmp_fname(employee.getEmp_fname());
        }
        if (!employee.getEmp_lname().equals("")&&employee.getEmp_lname()!=null){
            empToBeUpdated.setEmp_lname(employee.getEmp_lname());
        }
        if (!employee.getEmp_email().equals("")&&employee.getEmp_email()!=null){
            empToBeUpdated.setEmp_email(employee.getEmp_email());
        }
        if (employee.getEmp_dob()!=null){
            empToBeUpdated.setEmp_dob(employee.getEmp_dob());
        }

        if (employee.getSkills()!= null){
            empToBeUpdated.setSkills(employee.getSkills());
        }

        repository.save(empToBeUpdated);

    }

Here is the error I'm getting

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of java.util.HashSet (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (''); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of java.util.HashSet (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('') at [Source: (PushbackInputStream); line: 1, column: 94] (through reference chain: com.ems.assignment1.model.Employee["skills"])]

2
  • I think the issue is you missed the default constructor. Commented Jul 15, 2019 at 7:20
  • @gnanajeyam95 No I tried that as well.It didn't worked Commented Jul 15, 2019 at 7:31

1 Answer 1

5

The problem I faced was, the back end expect a skill set but if when the user didn't pick any skills from the skill list the front end passing an empty string. I resolved this issue by sending an empty array from angular front end if the user didn't select any skills from the skill list.

constructor(private apiService: ApiService) {
    this.nullSkills = [];
  }

if (this.form.controls.skillsSet.value == null || this.form.controls.skillsSet.value === '') {
      this.employee.skills = this.nullSkills;
    } else {
      this.employee.skills = this.form.controls.skillsSet.value;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.