首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘一对多’/‘多到多’属性值类型不应该是'?延展

问‘一对多’/‘多到多’属性值类型不应该是'?延展
EN

Stack Overflow用户
提问于 2020-03-22 11:03:59
回答 1查看 6.8K关注 0票数 10

我试图使用Kotlin中的注释来定义一个简单的Hibernate映射。但我的多对多关系并没有像预期的那样起作用。导致IntelliJ IDEA中出现以下错误:

‘一对多’/‘多到多’属性值类型不应该是'?扩展PersonAddress/Person‘

我的代码库:

代码语言:javascript
复制
@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关系进行了相同的尝试:

代码语言:javascript
复制
 @ManyToMany(mappedBy = "addresses")
var persons: Set<Person> = setOf()

但同样的错误也发生在这里。

Application.kt

代码语言:javascript
复制
@SpringBootApplication
class Application {
    fun main(args: Array<String>) {
        runApplication<Application>(*args)
    }
}

build.gradle.kts

代码语言:javascript
复制
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:我通过使用以下命令消除了错误:

代码语言:javascript
复制
var persons: MutableList<PersonAddress> = mutableListOf()

但是,在我的代码中,错误发生在昨天运行良好的属性上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-22 12:27:06

所以要回答我自己的问题:

由于我使用的是open类,所以列表必须是一个可变集合。错误信息可能会更好。

代码语言:javascript
复制
@OneToMany(mappedBy = "address")
var persons: MutableList<PersonAddress> = mutableListOf()
票数 33
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60798514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档