这是我的学校实体。
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class School{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long id;
@Column
private String name;
@JsonManagedReference
@OneToMany(mappedBy = "school", cascade = CascadeType.ALL)
private Set<Teacher> Teachers= new HashSet<>();
//constructor, getter and setter
这是我的老师课
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Teacher{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long id;
@Column
private String userId;
@ManyToOne
@JoinColumn(name = "SCHOOL_ID")
@JsonProperty("schoolId")
@JsonBackReference
private School school;
//constructor, getter and setter
我的老师看起来像这样
@ApiOperation(value = "Create new teacher", response = Teacher.class)
@ApiParam
@RequestMapping(value = "/teacher", method = RequestMethod.POST)
public Teacher createTeacher(@RequestBody Teacher t) {
return teacherService.saveTeacher(t);
}
我的服务只是调用crudepository save方法。
public Teacher saveTeacher(Teacher t) {
return teacherRepository.save(t);
}
这就是我的json在创建教师时的样子。my db中存在id为1的学校对象
{
"userId":"jDoe",
"schoolId":1
}
这就是我得到的错误
Could not resolve parameter [0] in public com.lace.schoolApp.model.Teacher com.lace.schoolApp.controller.TeacherController.createTeacher(com.lace.schoolApp.model.Teacher): JSON parse error: Unresolved forward references for: ; nested exception is com.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:
at [Source: (PushbackInputStream); line: 7, column: 1]Object id [1] (for `com.lace.schoolApp.model.School`) at [Source: (PushbackInputStream); line: 5, column: 16].
2021-02-10 17:04:11.959 WARN 11628 --- [nio-8090-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unresolved forward references for: ; nested exception is com.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for:
at [Source: (PushbackInputStream); line: 7, column: 1]Object id [1] (for `com.lace.schoolApp.model.School`) at [Source: (PushbackInputStream); line: 5, column: 16].]
2条答案
按热度按时间vuktfyat1#
你需要使用
schoolId
传入来自的请求负载scholRepository
你的第一个teacherService
.然后将学校对象设置为教师并保存
798qvoo82#
您可以使用另一个字段作为参考,如schoolid:
带请求正文: