Hi a newbie here learning Kotlin and Spring, and I...
# spring
j
Hi a newbie here learning Kotlin and Spring, and I have some questions regarding writing
@Entity
class: 1. Should one NOT use
data class
for defining
@Entity
class? I’ve found some articles which say that one shouldn’t, but then I’ve also seen many sources just using them. 2. When defining the properties for an
@Entity
class, is there a rule to be followed for either using
val
or
var
? 3. For
@Id
@GeneratedValue
property, what is the better way to define it: either as a nullable property initialized with
null
? or as a non-nullable property initialized with other dummy value?
d
Concerning 1: I am not an expert on this topic and am not sure of this is correct for Kotlin data classes. It might be similar to the discussion for using @Data from Lombok in java for entity classes. https://mdeinum.github.io/2019-02-13-Lombok-Data-Ojects-Arent-Entities/ The generated hashCode Method / equals check will check object properties and might consider two entities not equal, eventho they might point to the same entry in the database. Equals should be based on the ID. And the toString method might read all the properties, causing eager fetching of collections. It is 'cleaner' to have your own equals/hashcode implementation based on the ID and a simpler toString in some sort of BaseEntitiy. Of course, if you are not depending on the equash/hashcode checks and not using toString, those problems would not matter. I'd love to hear from others on this topic myself. 🙂