我试图使用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因此,由于我认为这可能是联接表导致的错误,所以我对ManyToMany关系进行了相同的尝试:
@ManyToMany(mappedBy = "addresses")
var persons: Set<Person> = setOf()但同样的错误也发生在这里。
Application.kt
@SpringBootApplication
class Application {
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
}build.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()但是,在我的代码中,错误发生在昨天运行良好的属性上。
发布于 2020-03-22 12:27:06
所以要回答我自己的问题:
由于我使用的是open类,所以列表必须是一个可变集合。错误信息可能会更好。
@OneToMany(mappedBy = "address")
var persons: MutableList<PersonAddress> = mutableListOf()https://stackoverflow.com/questions/60798514
复制相似问题