为具有多对一关系的列保存对象错误



这是我的学校实体。

@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);
}

我的服务只调用CrudRepository保存方法。

public Teacher saveTeacher(Teacher t) {
return teacherRepository.save(t);
}

这是我的JSON在创建教师时的样子。数据库中存在id = 1的school对象

{
"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].]

您需要在teacherService中首先使用scholRepository在请求有效载荷中传递的schoolId获取学校详细信息。
然后设置学校对象为Teacher并保存

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TeacherService{
@Autowired
private SchoolRepository schoolRepository;
@Autowired
private TeacherRepository teacherRepository;
@Transactional
public Teacher saveTeacher(Teacher t) {
School school = schoolRepository.findById(t.getschool().getId()); // schoolId from request //Fetch school details
t.setSchool(school); // set school details.
return teacherRepository.save(t); // save teacher details with existing school
}
}
{
"userId":"jDoe",
"schoolId":{
"id":1
}
}

您可以使用其他字段作为参考,如schoolId:

@Entity
public class Teacher{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long id;
@Column
private String userId;
@Column(name="school_id", insertable = false, updatable = false)
@JsonProperty("schoolId")
private Long schoolId;
@ManyToOne
@JoinColumn(name = "school_id")
private School school;

请求正文:

{
"userId": "jDoe",
"schoolId": 1
}

最新更新