Pihentagy
09/26/2024, 2:29 PMJohan
09/26/2024, 2:34 PMPihentagy
09/26/2024, 2:36 PMJohan
09/26/2024, 2:37 PMJohan
09/26/2024, 2:43 PMPihentagy
09/26/2024, 2:44 PMPihentagy
09/26/2024, 2:44 PMPihentagy
09/27/2024, 3:43 PM@Convert(converter = Converter::class)
does not solve the issue.
My setup:
@JvmInline value class LicensePlate private constructor(val value: String) {
companion object {
fun fromString(s: String): LicensePlate = LicensePlate(s.replace("-", ""))
}
}
@Converter
class LicensePlateConverter : AttributeConverter<LicensePlate?, String?> {
override fun convertToDatabaseColumn(value: LicensePlate?): String? = value?.value
override fun convertToEntityAttribute(value: String?): LicensePlate? = value?.let { LicensePlate.fromString(it) }
}
interface VehicleRepository : JpaRepository<Vehicle, LicensePlate> {
fun deleteByRegnumNotIn(regnums: List<LicensePlate>)
}
@Entity(name = "vehicles")
class Vehicle(
@Convert(converter = LicensePlateConverter::class)
@Column(name = "_id", columnDefinition = "character varying(20)")
@Id
var regnum: LicensePlate,
...
)
org.springframework.orm.jpa.JpaSystemException: Could not convert 'xxx.personnel.LicensePlate' to 'java.lang.String' using 'org.hibernate.type.descriptor.java.StringJavaType' to wrap
during a call to
val vehicleList = vehicleRepo.findAllById(regnums)
Johan
09/27/2024, 3:54 PMPihentagy
09/27/2024, 4:24 PMPihentagy
09/27/2024, 4:48 PM@JvmInline
value class LicensePlate private constructor(
val value: String) {
companion object {
fun fromString(s: String): LicensePlate = LicensePlate(s.replace("-", ""))
}
}
data class VehiclePK(
@Convert(converter = LicensePlateConverter::class)
@Column(name = "_id", columnDefinition = "character varying(20)")
val regnum: LicensePlate
) : Serializable
@Entity(name = "vehicles")
@IdClass(VehiclePK::class)
class Vehicle(
@Id
var regnum: LicensePlate,
interface VehicleRepository : JpaRepository<Vehicle, VehiclePK> {
fun deleteByRegnumNotIn(regnums: List<LicensePlate>)
}
but still getting
org.springframework.orm.jpa.JpaSystemException: Error attempting to apply AttributeConverter: class java.lang.String cannot be cast to class hu.icontest.personnel.LicensePlate (java.lang.String is in module java.base of loader 'bootstrap'; xxx.personnel.LicensePlate is in unnamed module of loader 'app')
during a call of
val vehicleList = vehicleRepo.findAllById(regnums)
Szymon Jeziorski
09/30/2024, 10:36 AMvalue class
? JPA is written to primarily work with Java classes, so I wouldn't be surprised for issues to arise when you use strictly Kotlin specific things like inline classes.
Also, if you're bringing in JPA and all its machinery, I'm pretty confident performance advantage of using inline class here is negligible.
I mean, if you're doing this for fun, than go for it, but if that's some real problem you're trying to solve, I would just go for normal data class
(or even type alias or plain String if you don't have any other logic around LicensePlate
class and just use it as an alias)Pihentagy
09/30/2024, 10:39 AMPihentagy
09/30/2024, 10:39 AMSzymon Jeziorski
09/30/2024, 11:00 AMLicensePlate
creation to custom factory function so value class might indeed be a good fit for it. Didn't notice that earlier.
That being said, I still think you might be better off using normal data class for this scenario, to save yourself a headache of fighting with JPA not being compatible with inline classes.
Also, looks like issue for this was raised on spring data commons Github, you might want to take a look at it:
https://github.com/spring-projects/spring-data-commons/issues/2868Pihentagy
09/30/2024, 11:03 AM