StackOverflowError 与 Kotlin 中的 JPA 双向引用

StackOverflowError with JPA bidirectional references in Kotlin

我有如下数据类:

@Entity
@Table(name = "SECTIONS")
data class Section(

        @Id @GeneratedValue
        @Column(name = "ID")
        var id: Long = 0,

        @Column(name = "NAME")
        var name: String = "",

        @OneToMany(
                mappedBy = "section",
                fetch = FetchType.EAGER,
                cascade = arrayOf(CascadeType.ALL),
                orphanRemoval = true
        )
        var fields: MutableList<Field> = mutableListOf()
)

@Entity
@Table(name = "FIELDS")
data class Field(

        @Id @GeneratedValue
        @Column(name = "ID")
        var id: Long = 0,

        @Column(name = "NAME")
        var name: String = "",

        @ManyToOne
        @JoinColumn(name = "SECTION_ID")
        var section: Section? = null
)

如你所见,Section和Field之间是双向映射的。当我创建一个 Section 对象、一个 Field 对象并将 Field 对象添加到 Section 对象中的字段列表时,它工作正常。但是,当我还将 Field 的部分引用设置为 Section 对象然后坚持时,我得到一个 WhosebugError:

@Test
fun testCascadeSaving() {
    val section = Section(name = "Section 1")
    val field = Field(name = "Field 1")

    section.fields.add(field)
    field.section = section

    val savedSection = sectionRepository.save(section)
    val savedField = savedSection.fields[0]

    // This causes an WhosebugError
    val f = fieldRepository.findOne(savedField.id)
}

我必须注释 field.section = section 行,以便上面的代码正常工作。

知道为什么设置双向关系会导致此错误吗?

我实际上已经设法解决了这个问题 - 我所要做的就是在至少一个实体中覆盖 toString() 方法。 Kotlin 提供的实现包括递归调用彼此的 toString() 方法,从而导致 WhosebugError.

@Entity
@Table(name = "FIELDS")
data class Field(

        @Id @GeneratedValue
        @Column(name = "ID")
        var id: Long = 0,

        @Column(name = "NAME")
        var name: String = "",

        @ManyToOne
        @JoinColumn(name = "SECTION_ID")
        var section: Section? = null
) {
    override fun toString(): String {
        return "Field(id=$id, name=$name)"
    }
}