我是Spring DataJPA + REST项目的新手,我正在尝试使用OneToOne单向关系从引用EmployerType执行添加新的或编辑Employer。任务很简单,但我卡住了。当我尝试添加或编辑数据时,我收到此错误:
org.springframework.http.converter.HttpMessageNotReadableException: 无法读取文件:无法构建实例 tr.mis.domain.EmployerType:否字符串参数构造函数/工厂 在[来源:来源:从字符串值('1074')反序列化的方法: java.io.PushbackInputStream@6d856887; line:1,column:609](通过 参考链:tr.mis.domain.Employer [“employerType”]);嵌套 异常是com.fasterxml.jackson.databind.JsonMappingException:可以 不构造tr.mis.domain.EmployerType的实例:不 从String反序列化的String-argument构造函数/工厂方法 价值('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
EmployerType类
@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
休息控制器
@RequestMapping(value = "/employers", method = RequestMethod.PUT)
public Employer updateEmployer(@RequestBody Employer employer) {
return employerRepository.save(employer);
}
答案 0 :(得分:4)
我通过在EmployerType类中添加String构造函数来修复此问题。
public EmployerType(String employerTypeName) {
// TODO Auto-generated constructor stub
}