spring - “一对多”/“多对多”属性值类型不应该是“?延伸'
问题描述
我正在尝试使用 Kotlin 中的注释定义一个简单的 Hibernate 映射。但是我的多对多关系没有按预期工作。在 IntelliJ IDEA 中导致以下错误:
“一对多”/“多对多”属性值类型不应该是“?扩展 PersonAddress/Person'
我的代码库:
@Entity
open class Person(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
var id: Long = 0L,
@OneToMany(mappedBy = "person")
var addresses: Set<PersonAddress> = setOf() //Fail
)
@Entity
open class Address(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
var id: Long = 0L,
@OneToMany(mappedBy = "address")
var persons: Set<PersonAddress> = setOf() //Fail
)
@Entity
open class PersonAddress(
@Id
var id: Long,
@ManyToOne
@JoinColumn(name = "person_id")
var person: Person,
@ManyToOne
@JoinColumn(name = "address_id")
var address: Address
) : Serializable
因此,当我认为这可能是由 Join-Table 引起的错误时,我对ManyToMany
Relation 进行了相同的尝试:
@ManyToMany(mappedBy = "addresses")
var persons: Set<Person> = setOf()
但是这里发生了同样的错误。
应用程序.kt
@SpringBootApplication
class Application {
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
}
构建.gradle.kts
plugins {
java
id("org.springframework.boot") version "2.2.5.RELEASE"
id("io.spring.dependency-management") version "1.0.9.RELEASE"
kotlin("jvm")
kotlin("plugin.spring") version "1.3.70"
kotlin("plugin.jpa")
kotlin("plugin.allopen") version "1.3.70"
}
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}
allOpen {
annotation("javax.persistence.Entity")
annotation("javax.persistence.Embeddable")
annotation("javax.persistence.MappedSuperclass")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks {
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
编辑1:我通过使用摆脱了错误:
var persons: MutableList<PersonAddress> = mutableListOf()
然而,通过我的代码,错误发生在昨天运行良好的属性上。
解决方案
所以回答我自己的问题:
由于我正在使用open
类,因此 List 必须是一个可变集合。错误信息可能会更好。
@OneToMany(mappedBy = "address")
var persons: MutableList<PersonAddress> = mutableListOf()
推荐阅读
- javascript - 在事件处理程序中引用 `this` vue 组件
- node.js - 我的电子商务网站出现错误,“'X' is not defined no-undef”
- kubernetes - 为 Minikube Ingress 启用 SSLv3
- .net - 我可以使用 powershell 删除预编译的 .NET C# dll 的清单吗
- java - 使用 Spring Boot 下载 .xls 文件 y Apache POI 不起作用
- ios - UIImageView 圆形 swift
- c# - 嵌套的异步方法太多。那是问题吗?
- python - 如何在 Django 中创建链接以使其他用户无法访问它们?
- azure - 如何在函数应用中引用 Azure Key Vault 中的密钥?
- java - 导入android.support.v4.app.ActivityCompat;