Hi everyone! - when we are using Kotlin data class...
# getting-started
s
Hi everyone! - when we are using Kotlin data class with javax.persistence entity, does it make sense to have something like
Copy code
@Column(nullable = false)
    val locationId: String?,
- if the db schema defines not null, does it make sense to mark the data class field as nullable? What is the behaviour in such a case?
a
Can the row be created/modified to include null value on that column? If it can’t be created/modified with a null value at all, then it wouldn’t make sense to make it nullable. If the null contract is violated on DB side, you will indeed crash at runtime.
s
I get it, so it's an extra safety in case the db violates nullable. Not too sure how @Column(nullable = false) would behave in that scenario. Are there any performance implications of using String? if it's unnecessary?
a
Underneath it all on the JVM,
Any?
compiles to a null check and throwing exception if it wasn’t null. The performance impact is barely there (next to nil) since its just basic boolean logic but the overhead does exist. In contrast, the programming overhead of handling
T
vs
T?
is up to you, the human 👨‍💻
k
Actually, if your value is very rarely null the JVM doesn't even compile the null check and catches segfaults instead, so there isn't even any cost to it: http://jpbempel.blogspot.com/2013/09/null-check-elimination.html?m=1
👍 3