Micronaut data-jpa - one-to-many - Child 实体没有 parent 引用

Micronaut data-jpa - one-to-many - Child entity don't have parent reference

我正在使用 micronaut-data-hibernate-jpa 并且我有 one-to-many 关系 Parent-Children。 我试图通过对 parent 存储库的保存操作来保存 parent 实体和 children 列表。 Parent 和 children 已保存,但 child 没有 parent id 保存在数据库中。

我的实体定义如下:

import java.util.*
import javax.persistence.*

@Entity
data class Parent(
    @Id
    @GeneratedValue
    var id: UUID? = null,
    var name: String,
    @OneToMany(mappedBy = "parent",cascade = [CascadeType.ALL])
    var children: List<Child> = listOf(),
    )
@Entity
data class Child(
    @Id
    @GeneratedValue
    var id: UUID? = null,
    var name: String = "",
    @ManyToOne(fetch = FetchType.EAGER, cascade = [CascadeType.ALL])
    var parent: Parent? = null,
)

存储库是使用 CrudRepository 创建的

import io.micronaut.data.annotation.Repository
import io.micronaut.data.repository.CrudRepository
import java.util.*

@Repository
interface ParentRepository : CrudRepository<Parent, UUID>

@Repository
interface ChildRepository : CrudRepository<Child, UUID>

我正在使用简单测试来检查我的设置是否正常,但 apparent只是有问题


import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

@MicronautTest
class OneToManyTest(
    private val parentRepository: ParentRepository,
    private val childRepository: ChildRepository,
) {

    @BeforeEach
    fun setUp() {
        val childA = Child(name = "Child A")
        val childB = Child(name = "Child B")
        val parentOne = Parent(name = "Parent 1", children = listOf(childA, childB))
        parentRepository.save(parentOne)

        val parentTwo = Parent(name = "Parent 2")
        val childC = Child(name = "Child C", parent = parentTwo)
        childRepository.save(childC)
    }

    @AfterEach
    fun clean() {
        childRepository.deleteAll()
        parentRepository.deleteAll()
    }

    @Test //THIS TEST FAILS
    fun `Parent should have child when saved by parent`() {
        val actual = parentRepository.findAll().toList().filter { it.name == "Parent 1" }[0]

        assertEquals(2, actual.children.size)
    }

    @Test //THIS TEST FAILS
    fun `Child should have parent when saved by parent`() {
        val actual = childRepository.findAll().toList().filter { it.name != "Child C" }

        assertEquals(2, actual.size)  // THIS ASSERT IS OK
        assertNotNull(actual[0].parent)
        assertNotNull(actual[1].parent)
    }

    @Test //THIS TEST IS FINE
    fun `Parent should have child when saved by child`() {
        val actual = parentRepository.findAll().toList().filter { it.name == "Parent 2" }[0]

        assertEquals(1, actual.children.size)
    }

    @Test //THIS TEST IS FINE
    fun `Child should have parent when saved by child`() {
        val actual = childRepository.findAll().toList().filter { it.name == "Child C" }[0]

        assertNotNull(actual.parent)
    }
}

我不知道这是否相关,但我使用 Flyway 生成表格并且我使用的是 postgres

我的迁移脚本:

CREATE
EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE parent
(
    id   UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    name CHARACTER VARYING NOT NULL
);

CREATE TABLE child
(
    id        UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    name      CHARACTER VARYING NOT NULL,
    parent_id UUID REFERENCES parent (id)
)

您声明了 mappedBy,它定义了谁是关系的所有者,在您的情况下是 Child,因此您应该在此处分配 parent。