2014-10-04 13 views
5

我發現kotlin中的數字不可序列化。kotlin中的數字不可序列化

  1. 第一個問題

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> 
} 

我得到一個錯誤,當我嘗試編譯這段代碼,因爲kotlin.Long不是序列化

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

  • 問題二
  • 我得到同樣的錯誤,當我嘗試使用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?'

    +1

    雖然您的解決方法已經足夠好了(並且祝賀您找到它!),但這是Kotlin基礎架構中的一個問題,我們將盡最大努力解決該問題。 請觀看此問題以獲取有關我們進度的通知:https://youtrack.jetbrains.com/issue/KT-5821 – 2014-10-05 05:36:50

    +0

    AndreyPaslavsky感謝您的解決方法, @AndreyBreslav,很高興聽到這將得到解決。 如果你可以請澄清爲什麼java.lang.Long不可序列化從Kotlin透視,而它是Java中的Serializable?謝謝。 – beegor 2014-10-16 13:45:17

    +0

    @beegor,'java.lang.Long'不可序列化,因爲Kotlin認爲它是不可序列化的'kotlin.Number'(而不是'java.lang.Number')的子類。這個映射'j.l.Number - > k。Number'是使Kotlin/Java interop工作順利進行的必要條件,當Kotlin調用Java方法 – 2014-10-16 18:33:59

    回答

    0

    我偶然發現了同樣的問題,並設法通過在java中使用我的Repository接口來處理它,在那裏我將java.lang.Long作爲id的泛型類型參數。其餘的則留在科特林(數據類,服務類等)

    1

    我發現這個問題的解決方法:

    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> 
    } 
    

    這個用例工作正常

    相關問題