4

I am new to Spring DataJPA + REST project and I am trying to perform add new or edit Employer from reference EmployerType with OneToOne unidirectional relationship.The task is simple, but I am stuck. When I try to add or edit data I am getting this error:

org.springframework.http.converter.HttpMessageNotReadableException:
Could not read document: Can not construct instance of
tr.mis.domain.EmployerType: no String-argument constructor/factory
method to deserialize from String value ('1074')  at [Source:
java.io.PushbackInputStream@6d856887; line: 1, column: 609] (through
reference chain: tr.mis.domain.Employer["employerType"]); nested
exception is com.fasterxml.jackson.databind.JsonMappingException: Can
not construct instance of tr.mis.domain.EmployerType: no
String-argument constructor/factory method to deserialize from String
value ('1074')

Below is the information about the classes.

@Entity
@Table(name = "employer")
public class Employer implements Serializable  {
    @GenericGenerator(
            name = "wikiSequenceGenerator",
            strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = {
                    @Parameter(name = "sequence_name", value = "WIKI_SEQUENCE"),
                    @Parameter(name = "initial_value", value = "1000"),
                    @Parameter(name = "increment_size", value = "1")
            }
    )
    @Id
    @GeneratedValue(generator = "wikiSequenceGenerator")
    private Long employerId;
    private String uid;
    private String name;
    private String address;
    private String headName;
    
    //@JsonIgnore
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   // @PrimaryKeyJoinColumn
    @JoinColumn(name = "employer_type_id")
    private EmployerType employerType;

    //Getters and Setters

EmployerType class

 @Entity
    @Table(name = "employertype")
    
    public class EmployerType {
        @GenericGenerator(
                name = "wikiSequenceGenerator",
                strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
                parameters = {
                        @Parameter(name = "sequence_name", value = "WIKI_SEQUENCE"),
                        @Parameter(name = "initial_value", value = "1000"),
                        @Parameter(name = "increment_size", value = "1")
                }
        )
        @Id
        @GeneratedValue(generator = "wikiSequenceGenerator")

        private Long employerTypeId; 
        private String uid; 
        private String employerTypeName;
    
    //Getters and Setters

Rest Controller

@RequestMapping(value = "/employers", method = RequestMethod.PUT)
    public Employer updateEmployer(@RequestBody Employer employer) {

        return employerRepository.save(employer);
        
    }
Guildenstern
  • 2,179
  • 1
  • 17
  • 39
Ruslan
  • 101
  • 1
  • 5
  • See my related questions: [1](https://stackoverflow.com/questions/45620195/spring-data-rest-put-request-does-not-work-properly-since-v-2-5-7), [2](https://stackoverflow.com/questions/41324078/spring-data-rest-can-not-update-patch-a-list-of-child-entities-that-have-a-r), maybe they can help... – Cepr0 Mar 30 '18 at 07:15
  • Just remove `@JoinColumn(name = "employer_type_id")`. It should work – atul ranjan Mar 30 '18 at 10:32

1 Answers1

5

I have fixed this issue by adding the String constructor in the EmployerType class.

 public EmployerType(String employerTypeName) {
        // TODO Auto-generated constructor stub
    }
Ruslan
  • 101
  • 1
  • 5