没有字符串参数的构造函数/工厂方法从字符串值('1074')反序列化。

4

我是Spring DataJPA + REST项目的新手,我试图执行从具有一对一单向关系的参考雇主类型 (EmployerType) 添加新雇主或编辑雇主的操作。这个任务很简单,但我卡住了。当我尝试添加或编辑数据时,我会遇到以下错误:

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')

以下是有关课程的信息。
@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

雇主类型类

 @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控制器

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

        return employerRepository.save(employer);
        
    }

请参考我的相关问题:12,也许它们能够帮到你。 - Cepr0
只需删除 @JoinColumn(name = "employer_type_id")。它应该可以工作。 - atul ranjan
1个回答

5
我已通过在EmployerType类中添加String构造函数来解决此问题。
 public EmployerType(String employerTypeName) {
        // TODO Auto-generated constructor stub
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接