Numbers in kotlin are not serializable

I found that the numbers in kotlin are not serializable.

  • First problem

Device.kt:

package test.domain import javax.persistence.* Entity public class Device { public Id GeneratedValue var id: Long = -1 public var name: String = "" ... } 

DeviceRestRepository.kt:

 package test.domain import org.springframework.data.repository.PagingAndSortingRepository import org.springframework.data.repository.query.Param import org.springframework.data.rest.core.annotation.RepositoryRestResource RepositoryRestResource(collectionResourceRel = "device", path = "device") public trait DeviceRestRepository : PagingAndSortingRepository<Device, Long?> { public fun findByName(Param("name") name: String): List<Device> } 

I get an error when trying to compile this code because kotlin.Long is not Serializable :

Error: (14, 72) Kotlin: the type argument is not within its borders: should it be a subtype of "java.io.Serializable?"

  1. Second problem

I get the same error when I try to use java.lang.Long :

DeviceRestRepository.kt:

 package test.domain import org.springframework.data.repository.PagingAndSortingRepository import org.springframework.data.repository.query.Param import org.springframework.data.rest.core.annotation.RepositoryRestResource RepositoryRestResource(collectionResourceRel = "device", path = "device") public trait DeviceRestRepository : PagingAndSortingRepository<Device, java.lang.Long?> { public fun findByName(Param("name") name: String): List<Device> } 

Warning: (14, 72) Kotlin: This class should not be used in Kotlin. use kotlin.Long instead.

Error: (14, 72) Kotlin: the type argument is not within its borders: should there be a subtype of "java.io.Serializable?"

+5
source share
3 answers

As-of Kotlin 1.0 Beta 1 primitive types are serializable:

Int is Serializable

Now the type is Int and the other base types are Serializable on the JVM. This should help many platforms.

from: http://blog.jetbrains.com/kotlin/2015/10/kotlin-1-0-beta-candidate-is-out/

Therefore, you have no more problems.

+3
source

I found a workaround for this problem:

Device.kt:

 package test.domain import javax.persistence.* Entity public class Device { public EmbeddedId var id: DeviceId = DeviceId() public var name: String = "" ... } Embeddable public class DeviceId: Serializable { public GeneratedValue var id: Long = -1 } 

DeviceRestRepository.kt:

 package test.domain import org.springframework.data.repository.PagingAndSortingRepository import org.springframework.data.repository.query.Param import org.springframework.data.rest.core.annotation.RepositoryRestResource RepositoryRestResource(collectionResourceRel = "device", path = "device") public trait DeviceRestRepository : PagingAndSortingRepository<Device, DeviceId?> { public fun findByName(Param("name") name: String): List<Device> } 

This use case works great

+1
source

I came across the same problem and I managed to deal with it using my repository interfaces in java, where I gave java.lang.Long as a generic type argument for id. The rest remained in kotlin (data classes, service classes, etc.).

0
source

Source: https://habr.com/ru/post/1204020/


All Articles