Itself java.util.Date
is a mutable object. Thus, even if the Kotlin data class (with val date declaration) does not allow me to change the link, I can change the date object itself to change its value.
Ways I could come up with:
Use normal class, override getter and setter. In each case, use the clone method to create a copy of the given date.
@Column(name = "db_date")
private var dbDate: Date? = null
get() = dbDate?.clone() as Date
set(date) {
field = date?.clone() as Date
}
Also, I cannot use the copy
data class method , since these classes are sleeping entities. So I need to change them through the setters.
The reason I want to use data classes for my objects is because by default they implement equals
and hashcode
. We used lombok in java for this, and now a convincing command to create these methods is complicated. Even if the generation is done using the IDE, it will still be checked for source control.
So that I can make user settings in the logic of the data class. Or in any way, can I generate equal and hash codes for regular classes without checking them in the source control?
: java.time.Instant
, . , , , Hibernate Entity Class, hibernate 3.6. 5.2, , . , , , kotlin -. :
@Entity
@Table(name = "my_table")
data class MyTable(
@Id
@Column(name = "id")
var id: Long? = null,
@Column(name = "my_date")
private var date: Date? = null,
) {
fun getDate():Date = gradedDate?.clone() as Date
fun setDate(date: Date?) {
this.date = date?.clone() as Date
}
}