JPA 无法检索父子映射的子条目

JPA unable to retrieve child entries for parent-child mapping

我正在尝试在同一个 class:

中映射父子关系
public class Category {        

    @Id
    @SequenceGenerator(name = "categorySeq", sequenceName = "category_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "categorySeq")

    @Column(name = "category_id", nullable = false)
    private Long id;

    @Column(name = "code", nullable = true)
    private String code;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "parent_id", nullable = true)
    private Long parentId;

    @ManyToOne
    @JoinColumn(name = "parent_id", insertable = false, updatable = false)
    public Category parentCategory;

    @OneToMany(mappedBy = "parentCategory", cascade = CascadeType.ALL)
    public List<Category> subCategories = new ArrayList<Category>();

}

当我在类别上执行 GET 时,我只得到 parentCategory 和 subCategories 结果为 null。我希望在 GET 上返回父条目和子条目。知道这里出了什么问题吗?

好吧,在关系的另一面,我不得不通过设置 FetchType.EAGER 急切地获取它,它检索子条目,但现在 Jackson 抱怨无限循环,我不得不使用 @JsonIgnore。但是,如果我使用@JsonIgore,它就违背了这个问题的目的,因为我需要查看子条目。

但我想我意识到要解析整个层次结构树,我可以通过递归遍历每个子项的父项来实现。

@OneToMany(mappedBy = "parentCategory", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public List<Category> subCategories = new ArrayList<Category>();