在ManyToMany关系中获取Set时的StackOverFlow

时间:2019-04-06 11:36:52

标签: java hibernate jpa spring-data-jpa

提取具有StackOverFlowException关系的一组实体时,我得到了ManyToMany

使用ModelMapper在映射层中引发了异常(使用@JsonIgnore并不能解决问题)。但是,调试时,我可以在collection字段中查看stackoverflow。

以下是我的代码:

@Getter
@Setter
@Entity
@Table(name = "a_child")
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AChildEntity extends AbstractEntity{

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

    @Column(length = 1024)
    private String description;

    @Column(nullable = false)
    private BigDecimal value;

    @ManyToMany(mappedBy = "a_child")
    private final Set<AnocherChildEntity> menu = new HashSet<>();

}
@Data
@Entity
@Table(name = "another_child")
@SuperBuilder
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AnotherChildEntity extends AbstractEntity{

    @Column(length = 255)
    private String name;

    @ManyToMany(cascade = {
        CascadeType.PERSIST,
        CascadeType.MERGE
    })
    @JoinTable(name = "another_child_a_child",
        joinColumns = @JoinColumn(name = "another_child_id"),
        inverseJoinColumns = @JoinColumn(name = "a_child_id")
    )
    private final Set<AChildEntity> aChild = new HashSet<>();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "other_id")
    private OtherEntity other;
}

在我的PersistenSet中,我得到: Exception occurred: com.sun.jdi.InvocationException occurred invoking method..,并且控制台日志中的多个类似消息使映射崩溃:

at java.util.AbstractCollection.addAll(AbstractCollection.java:344) ~[na:1.8.0_181]
    at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:236) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:223) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:196) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:249) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:212) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]

2 个答案:

答案 0 :(得分:0)

您有一个循环引用问题:AChildEntity引用集为AnocherChildEntity,而AnocherChildEntity引用集为AChildEntity。因此,在封送处理时会创建无限递归,从而导致StackOverFlowError。

用户@JsonIgnore在一侧,或者更好地使用没有循环引用的单独DTO类作为接口对象

答案 1 :(得分:0)

正如@JBNizet所建议的,我已经删除了两个实体中的@EqualsAndHashCode,并且循环引用也消失了。