Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbers in kotlin is not serializable

I found that numbers in kotlin is not serializable.

  1. 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 I try to compile this code, because kotlin.Long is not Serializable:

Error:(14, 72) Kotlin: Type argument is not within its bounds: should be 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 shouldn't be used in Kotlin. Use kotlin.Long instead.

Error:(14, 72) Kotlin: Type argument is not within its bounds: should be subtype of 'java.io.Serializable?'

like image 464
Andrey Paslavsky Avatar asked Sep 30 '22 17:09

Andrey Paslavsky


2 Answers

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

Int is Serializable

Now the type Int and other basic types are Serializable on the JVM. This should help many frameworks.

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

Therefore you no longer have any issues.

like image 154
Jayson Minard Avatar answered Oct 29 '22 01:10

Jayson Minard


I found workaround of 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 fine

like image 1
helperDude Avatar answered Oct 28 '22 23:10

helperDude