How to use Kotlin Null-Safe feature with Hibernate + Oracle empty Strings


#1

Hi Guys,

I want to use null-save value in JPA Entity. I can create this entity at database with Hibernate. Oracle can’t
distinguish empty string and null. It stores null anyway in table. When I try load this instance from database I have null value at firstName and secondName fields despite they are null-save

@Entity
class MyEntity {
    @Id 
    val int id;
    @Column
    var firstName: String = ""
    @Column
    var secondName: String = ""
}

Please advise how to resolve it


#2

Had you look at Converter?

See an example here:

https://javaee.github.io/javaee-spec/javadocs/javax/persistence/Convert.html


#3

@fvasco thank you it helped and works great! :slightly_smiling_face:

@Converter(autoApply = true)
class StringConverter : Attribute<String, String> {
  override fun convertToEntityAttribute(dbData: String?): String = dbData ?: ""
  override fun convertToDatabaseColumn(attribue: String?): String? = attribute
}

#4

You’re welcome