BabaAtWork (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiY2YyNWM5M2I0...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16736?atlOrigin=eyJpIjoiY2YyNW...
) HHH-16736 (
https://hibernate.atlassian.net/browse/HHH-16736?atlOrigin=eyJpIjoiY2YyNW...
) Empty ElementCollection using NonNullableConverter with Kotlin (
https://hibernate.atlassian.net/browse/HHH-16736?atlOrigin=eyJpIjoiY2YyNW...
)
Issue Type: Bug Affects Versions: 6.2.2 Assignee: Unassigned Components: hibernate-core
Created: 01/Jun/2023 05:26 AM Priority: Major Reporter: BabaAtWork (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
)
Hi all,
not sure if I am at the correct place to discuss this problem. I hope so.
I switched to Hibernate 6 with the newest Spring Boot 3.1.0 data-jpa starter. Before using
Spring Boot 2.7.11 the following problem was not there.
I use Kotlin here, which maybe makes a difference because of non nullable types.
I have the following in my Entity
@ElementCollection
@CollectionTable(name = "entity_description")
private val descriptionsInternal: MutableList<Description> = mutableListOf()
The Description class looks as follows
@Embeddable
data class Description(
@Column(nullable = false)
@Convert(converter = NonBlankString255AttributeConverter::class)
var desc: NonBlankString255,
@Column(nullable = false)
var order: Int,
)
I use a special AttributeConverter to convert the database string into Objects
sealed class StringAttributeConverter<T : Any>(
private val instanceCreator: (String) -> T,
) : AttributeConverter<T, String>
{ override fun convertToDatabaseColumn(attribute: T): String = attribute.toString()
override fun convertToEntityAttribute(dbData: String): T = instanceCreator(dbData) }
object NonBlankString255AttributeConverter :
StringAttributeConverter<NonBlankString255>(::NonBlankString255)
When selecting the Entity from database with left joining the descriptions, then i got a
Kotlin NullPointerException in "convertToEntityAttribute(dbData: String)"
because the dbData is null.
My database table for the descriptions is empty and I expect an empty descriptionsInternal
list without errors, because I not expect that the converter would be called for a field
that is not filled in db. At least this was not the case with an older version.
I could fix it by using
@Convert(converter = *Nullable*NonBlankString255AttributeConverter::class)
var desc: NonBlankString255,
instead of
@Convert(converter = NonBlankString255AttributeConverter::class)
var desc: NonBlankString255,
But this is not really nice, because the desc: NonBlankString255 is still a not nullable
Type without Kotlins "?"
Now I only can have Converters which need to handle always nullable values, because of a
maybe empty ElementCollection?
Thanks
(
https://hibernate.atlassian.net/browse/HHH-16736#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16736#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:40c4da4 )