5

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はSerializableではないため、このコードをコンパイルしようとするとエラーが発生します。

エラー:(14、72) Kotlin:型引数がその境界内にありません:「java.io.Serializable?」のサブタイプにする必要があります

  1. 第二の問題

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

警告:(14, 72) Kotlin: このクラスは Kotlin では使用しないでください。代わりに kotlin.Long を使用してください。

エラー:(14、72) Kotlin:型引数がその境界内にありません:「java.io.Serializable?」のサブタイプにする必要があります

4

3 に答える 3

3

Kotlin 1.0 Beta 1 の時点で、プリミティブ型はシリアル化可能です。

Int はシリアライズ可能

現在、型 Int およびその他の基本型は、JVM でシリアライズ可能です。これは多くのフレームワークに役立つはずです。

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

したがって、もう問題はありません。

于 2015-12-30T22:10:02.480 に答える
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>
}

このユースケースは正常に機能します

于 2015-09-17T10:30:56.467 に答える
0

私は同じ問題に遭遇し、ID のジェネリック型引数としてjava.lang.Longを指定した Java でリポジトリ インターフェイスを使用することで、なんとか対処しました。残りはkotlinに残りました(データクラス、サービスクラスなど)

于 2015-09-12T09:11:16.420 に答える