我正在使用 micronaut-data-hibernate-jpa 并且我有一對多的父子關系。我正在嘗試通過對父存盤庫的保存操作來保存帶有子項串列的父物體。父母和孩子被保存,但孩子沒有在資料庫中保存父母 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>
我正在使用簡單的測驗來檢查我的設定是否正常,但顯然存在問題
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)
)
uj5u.com熱心網友回復:
您宣告了mappedBywhich 定義了誰是關系的所有者,在您的情況下它是 Child,因此您應該在那里分配父級。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/347727.html
標籤:休眠 科特林 一对多 micronaut-数据
