java—为具有多对一关系的列保存对象时出错

lstz6jyr  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(354)

这是我的学校实体。

@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].]
vuktfyat

vuktfyat1#

你需要使用 schoolId 传入来自的请求负载 scholRepository 你的第一个 teacherService .
然后将学校对象设置为教师并保存

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
       }
}
798qvoo8

798qvoo82#

您可以使用另一个字段作为参考,如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
}

相关问题