'Need to serializable entity on kotlin
I have sombe BaseEntity
import org.springframework.data.util.ProxyUtils
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.MappedSuperclass
@MappedSuperclass
abstract class BaseEntity<T> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: T? = null
override fun equals(other: Any?): Boolean {
other ?: return false
if (this === other) return true
if (javaClass != ProxyUtils.getUserClass(other)) return false
other as BaseEntity<*>
return this.id != null && this.id == other.id
}
override fun hashCode() = 25
override fun toString(): String {
return "${this.javaClass.simpleName}(id=$id)"
}
}
And i have error message, but project are starting.
Class 'T' should implement 'java.io.Serializable'
I've heard that hibernate needs it. But I don't understand why he needs it. And I don't understand how I can do serialization of T
Solution 1:[1]
I fixed the problem by simply adding Serializable to type T.
BaseEntity<T: Serializable>
But I did not find out the reason for this error.
@MappedSuperclass
abstract class BaseEntity<T: Serializable> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: T? = null
override fun equals(other: Any?): Boolean {
other ?: return false
if (this === other) return true
if (javaClass != ProxyUtils.getUserClass(other)) return false
other as BaseEntity<*>
return this.id != null && this.id == other.id
}
override fun hashCode() = 25
override fun toString(): String {
return "${this.javaClass.simpleName}(id=$id)"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | WBLord |

