SpringData JPA插入数据时出现Stackoverflow错误

时间:2020-10-16 07:06:24

标签: spring-boot hibernate

我有两个实体。

SubjectEntity

 package com.crud.crudLearn.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Set;

@Entity
@Data
@Table
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SubjectEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private int subjectId;

    @Column
    private String subjectName;

    @ManyToMany(targetEntity = StudentEntity.class, mappedBy = "schoolEntities", fetch = FetchType.LAZY)
    @JsonIgnore
    private Set<StudentEntity> studentEntities;


}

和StudentEntity

    @Entity
@Data
@Table
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StudentEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private int userId;

    @Column
    private String userName;

    @ManyToMany(targetEntity = SubjectEntity.class, fetch = FetchType.LAZY)
    @JoinTable(name = "student_subject",
            joinColumns = {@JoinColumn(name = "student_id")},
            inverseJoinColumns = {@JoinColumn(name = "subject_id")})
    @JsonIgnore
    private Set<SubjectEntity> schoolEntities;
}

它具有ManytoMany的双向映射,现在当我尝试插入数据时,它会引发Stackoverflow错误。

  public void registerToSubjects(SubjectStudent subjectStudent) {
        if (subjectStudent == null) {
            return;
        }

        StudentEntity studentEntity = studentDAO.findById(subjectStudent.getStudentId()).orElse(null);
        if (studentEntity == null) return;

        SubjectEntity subjectEntity = subjectDAO.findById(subjectStudent.getSubjectID()).orElse(null);
        if (subjectEntity == null) {
            return;
        }

        Set<SubjectEntity> schoolEntities = new HashSet<>();

        schoolEntities = studentEntity.getSchoolEntities();
        schoolEntities.add(subjectEntity);

        studentDAO.save(studentEntity);
    }

我不明白为什么会这样?

错误堆栈有点像这样,很可能是由于循环引用而发生的,但是为此添加了Json忽略。

Hibernate: select studentent0_.user_id as user_id1_1_0_, studentent0_.user_name as user_nam2_1_0_ from student_entity studentent0_ where studentent0_.user_id=?
Hibernate: select subjectent0_.subject_id as subject_1_2_0_, subjectent0_.subject_name as subject_2_2_0_ from subject_entity subjectent0_ where subjectent0_.subject_id=?
Hibernate: select schoolenti0_.student_id as student_1_0_0_, schoolenti0_.subject_id as subject_2_0_0_, subjectent1_.subject_id as subject_1_2_1_, subjectent1_.subject_name as subject_2_2_1_ from student_subject schoolenti0_ inner join subject_entity subjectent1_ on schoolenti0_.subject_id=subjectent1_.subject_id where schoolenti0_.student_id=?
Hibernate: select studentent0_.subject_id as subject_2_0_0_, studentent0_.student_id as student_1_0_0_, studentent1_.user_id as user_id1_1_1_, studentent1_.user_name as user_nam2_1_1_ from student_subject studentent0_ inner join student_entity studentent1_ on studentent0_.student_id=studentent1_.user_id where studentent0_.subject_id=?
Hibernate: select schoolenti0_.student_id as student_1_0_0_, schoolenti0_.subject_id as subject_2_0_0_, subjectent1_.subject_id as subject_1_2_1_, subjectent1_.subject_name as subject_2_2_1_ from student_subject schoolenti0_ inner join subject_entity subjectent1_ on schoolenti0_.subject_id=subjectent1_.subject_id where schoolenti0_.student_id=?
Hibernate: select studentent0_.subject_id as subject_2_0_0_, studentent0_.student_id as student_1_0_0_, studentent1_.user_id as user_id1_1_1_, studentent1_.user_name as user_nam2_1_1_ from student_subject studentent0_ inner join student_entity studentent1_ on studentent0_.student_id=studentent1_.user_id where studentent0_.subject_id=?
Hibernate: select schoolenti0_.student_id as student_1_0_0_, schoolenti0_.subject_id as subject_2_0_0_, subjectent1_.subject_id as subject_1_2_1_, subjectent1_.subject_name as subject_2_2_1_ from student_subject schoolenti0_ inner join subject_entity subjectent1_ on schoolenti0_.subject_id=subjectent1_.subject_id where schoolenti0_.student_id=?
Hibernate: select studentent0_.subject_id as subject_2_0_0_, studentent0_.student_id as student_1_0_0_, studentent1_.user_id as user_id1_1_1_, studentent1_.user_name as user_nam2_1_1_ from student_subject studentent0_ inner join student_entity studentent1_ on studentent0_.student_id=studentent1_.user_id where studentent0_.subject_id=?

此查询将继续交替运行,直到给出SO。

这是完整的代码仓库:-https://github.com/alpitanand/CrudLearn

0 个答案:

没有答案