I'm trying to define a simple Hibernate Mapping using Annotations in Kotlin. But my Many-To-Many Relation is not working as expected. Causing the following error in IntelliJ IDEA:
'one to many' / 'many to many' attribute value type should not be '? extends PersonAddress/Person'
My codebase:
@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
So as I thought this might be an error caused by the Join-Table, I tried the same for a ManyToMany
Relation:
@ManyToMany(mappedBy = "addresses")
var persons: Set<Person> = setOf()
But the same error occures here.
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()
}
Edit 1: I got rid of the error by using:
var persons: MutableList<PersonAddress> = mutableListOf()
However walking through my code the error occures on attributes that worked well yesterday.