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.
‘One To Many’ attribute value type should not be ‘Ingredient’ Even though the line above it is working ? If you have a unidirectional association @OneToMany you need to replace
An instance of a single-valued attribute can contain a single value. An instance of a multivalued attribute can contain either a single value or multiple values. Active Directory does not create attributes with empty values—either the attribute contains a valid value or it does not exist on the object.
Because ADSI knows the syntax of attributes in the schema, you are not required to specify the attribute type when accessing it. ADSI marshals attribute values to the appropriate data type as defined in the schema. If your directory has no schema, supply the data type when accessing an attribute.
An instance of a multivalued attribute can contain either a single value or multiple values. Active Directory does not create attributes with empty values—either the attribute contains a valid value or it does not exist on the object. In Active Directory and most other LDAP servers, the order of values in a multi-valued attribute is undefined.
So to answer my own question:
Since I'm working with open
classes the List must be a mutable collection. The error message could be better.
@OneToMany(mappedBy = "address")
var persons: MutableList<PersonAddress> = mutableListOf()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With