如何使用 Spring Boot 和 Cassandra 将枚举持久化为序数?

2024-04-10

我已添加到实体的枚举字段@CassandraType(type = DataType.Name.INT)。然而,在发送到 Cassandra 的语句中使用的不是枚举的序数,而是字符串表示形式。因此我收到以下错误:

org.springframework.data.cassandra.CassandraInvalidQueryException: SessionCallback; CQL [INSERT INTO thing (thing_id,some_enum) VALUES (1,'Foo');]; Expected 4 or 0 byte int (3); nested exception is com.datastax.driver.core.exceptions.InvalidQueryException: Expected 4 or 0 byte int (3)

您可以在下面找到一个重现问题的最小示例。

我究竟做错了什么?

test/src/main/kotlin/enumtest/Application.kt

package enumtest

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication


@SpringBootApplication
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

test/src/main/kotlin/enumtest/SomeEnum.kt

package enumtest


enum class SomeEnum {
    Foo,
    Bar
}

test/src/main/kotlin/enumtest/Thing.kt

package enumtest

import com.datastax.driver.core.DataType
import org.springframework.data.cassandra.core.cql.PrimaryKeyType
import org.springframework.data.cassandra.core.mapping.CassandraType
import org.springframework.data.cassandra.core.mapping.Column
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn
import org.springframework.data.cassandra.core.mapping.Table


@Table("thing")
@Suppress("unused")
class Thing(

    @PrimaryKeyColumn(name = "thing_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    var thingId: Long,

    @CassandraType(type = DataType.Name.INT)
    @Column("some_enum")
    var someEnum: SomeEnum

)

test/src/main/kotlin/enumtest/ThingRepository.kt

package enumtest

import org.springframework.data.cassandra.repository.CassandraRepository
import org.springframework.stereotype.Repository

@Repository
interface ThingRepository : CassandraRepository<Thing, Long>

test/src/main/resources/application.yml

spring:
  data:
    cassandra:
      contact-points: localhost
      port: 9142
      keyspace_name: enumtest

test/src/test/kotlin/enumtest/PersistenceTest.kt

package enumtest

import org.cassandraunit.spring.CassandraDataSet
import org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener
import org.cassandraunit.spring.EmbeddedCassandra
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestExecutionListeners
import org.springframework.test.context.junit4.SpringRunner

@RunWith(SpringRunner::class)
@SpringBootTest
@TestExecutionListeners(
    listeners = [CassandraUnitDependencyInjectionTestExecutionListener::class],
    mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
@CassandraDataSet(value = ["cql/cassandra_schema.cql"], keyspace = "enumtest")
@EmbeddedCassandra
class PersistenceTest {
    @Autowired
    lateinit var thingRepository: ThingRepository

    @Test
    fun `test save`() {
        thingRepository.save(Thing(1, SomeEnum.Foo))
        val things = thingRepository.findAll()
        Assert.assertEquals(1, things.size)
        val thing = things[0]
        Assert.assertEquals(SomeEnum.Foo, thing.someEnum)
    }
}

test/src/test/resources/cql/cassandra_schema.cql

CREATE KEYSPACE IF NOT exists enumtest
WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1};

CREATE TABLE IF NOT exists enumtest.thing (
    thing_id     bigint,
    some_enum    int,
    PRIMARY KEY (thing_id)
);

test/build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'org.jetbrains.kotlin.jvm' version '1.3.30'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.3.30'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url "https://repository.apache.org/snapshots/" }
}

dependencies {
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-cassandra'
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8'
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect'

    testImplementation group: 'org.cassandraunit', name: 'cassandra-unit-spring', version: '3.5.0.1'
    testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}

compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '1.8'
    }
}

compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '1.8'
    }
}

以下是最小示例的完整版本,可供下载以方便实验:https://drive.google.com/open?id=1zzIDhbWycaj4WXrze2sAmw8xRPacA8Js https://drive.google.com/open?id=1zzIDhbWycaj4WXrze2sAmw8xRPacA8Js

编辑:因为这似乎是一个错误,所以我刚刚打开Jira 问题 https://jira.spring.io/browse/DATACASS-652.


我已经尝试让它工作很长一段时间了,看来我终于成功了!

我遇到了与编解码器相同的问题......我不知道为什么这不起作用。根据他们的文档,你做得完全正确。

所以我实现了自己的 Cassandra Write Converter。见下文

@Configuration
class CassandraConfig(val cluster: Cluster){

    @Bean
    fun setCustomCassandraConversions() = CassandraCustomConversions(listOf(EnumWriteConverter.INSTANCE, EnumReadConverter.INSTANCE))

    @WritingConverter
    enum class EnumWriteConverter : Converter<Enum<MyEnum>, Int> {
        INSTANCE;
        override fun convert(source: Enum<MyEnum>) = source.ordinal
    }
     @ReadingConverter
    enum class EnumReadConverter : Converter<Int, Enum<MyEnum>> {
        INSTANCE;
        override fun convert(source: Int) = MyEnum.values()[source]
    }
}

这应该在您对 Cassandra 进行的每次写入中使用覆盖的转换器将它看到的 MyEnum 类型的所有枚举转换为 Int。这为您提供了为不同类型的枚举提供多个这些的可能性,也许出于某种原因您想从中编写其他自定义值,而不是总是转换所有枚举。

希望这有效!

EDIT请注意删除每个转换器上 INSTANCE 的 { } 以及使用 CassandraCustomConversions 注册 ReadingConverter 的变化

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Spring Boot 和 Cassandra 将枚举持久化为序数? 的相关文章

随机推荐

  • c++;编译期间警告“默认启用”是什么意思?

    我的代码编译没有错误 但这意味着什么 etherate cpp 432 11 warning deleting array unsigned char broadMAC 6 enabled by default 从这一行开始 我删除了一个
  • 如何从 Facelets 和 web.xml 引用 JAR 中的 Facelets taglib?

    我正在使用 Apache MyFaces Commons验证器 http myfaces apache org commons20 myfaces validators20 tagdoc html TagLib 我应该如何将其添加到我的 w
  • 成员函数什么时候应该同时是 const 和 volatile ?

    我正在阅读有关易失性成员函数的内容 并发现成员函数可以同时是const 和 volatile 在一起 我没有得到这样的东西的真正用途 任何人都可以分享他们的经验吗实际使用将成员函数同时用作 const 和 volatile 我编写了小类来测
  • Amazon S3 - HTTPS/SSL - 可能吗? [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我看到了一些与此相关的其他问题 但没有任何真正的答案或信息 或者看起来如此 我这里有一张图片 http furniture retailcatalog
  • Django - 当文件等于 maxBytes 时旋转文件处理程序卡住

    我在使用 Django 时遇到 RotatingFileHander 问题 问题是 当文件达到 maxBytes 大小时 它不会创建新文件 并在您尝试执行 logger info any message 时给出错误消息 奇怪的部分是 没有人
  • 我们有 php 应用程序服务器(不是 Web 服务器)吗?

    对于 Java 我们有不同的应用程序服务器 例如 WebSphere 和 Web 逻辑 我的疑问是我们有 PHP 应用程序服务器吗 到目前为止 我们正在使用 Apache 和 Nginx Web 服务器来运行 PHP Web 应用程序 应用
  • 在 R Markdown 中使用环境中的数据[重复]

    这个问题在这里已经有答案了 我正在尝试使用来自全球环境的数据R Markdown 当我打电话给 summary mydata 它给了我这个错误 未找到对象 mydata 我的所有作品都包含在许多不同的脚本中 因此对我来说创建一个 R每个结果
  • Backbone - 对 id 使用不同的字段名称

    我正在移植一个旧应用程序以使用backbone js 希望如此 问题是系统中的各种对象都没有使用 id 作为 id 每个对象都是不同的 通过阅读 我在初始化 Backbone Model 时提出了以下解决方案 initialize func
  • 材质按钮上的圆角

    我正在遵循以下问题的提示this https stackoverflow com questions 26346727 android material design button styles创建一个像 Material Design 中
  • JavaScript 私有方法

    要使用公共方法创建 JavaScript 类 我会执行以下操作 function Restaurant Restaurant prototype buy food function something here Restaurant pro
  • 为什么scala人不喜欢注释?

    NET 中的属性是一个非常流行的功能 而Java在1 5之后添加了Annotation 注解随处可见 请参阅 Java EE 和 Spring 但很少有scala库使用注释 lift json 不要使用它 lift record 不要使用它
  • Python3:没有 gzip 或 zlib?

    该模块现在有其他名称吗 我似乎找不到任何信息表明该模块已被弃用或完全删除 这会让我更加困惑 有谁知道这去哪儿了 Python 3 1 2 r312 79147 May 20 2010 09 09 30 GCC 4 1 2 20080704
  • Ruby on Rails 最佳托管(截至 2012 年)[已关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 使用 FileHelpers 动态创建 CSV 文件

    FileHelpers 支持一项名为 运行时记录 的功能 当您直到运行时才知道布局时 该功能可让您将 CSV 文件读入数据表中 是否可以使用 FileHelpers 以相同的方式在运行时创建 CSV 文件 根据某些用户输入 必须创建的 CS
  • 结构体指针数组

    我试图了解我的代码是否正确 我需要声明一个指向结构的指针数组 创建一个新结构并分配值并打印它们 在我看来 我没有正确声明指针数组 我需要知道我做错了什么 谢谢 我收到此编译错误 错误 people 未声明 首次在此函数中使用 我尝试插入 s
  • 将小吃栏移动到底部栏上方

    我在新的底栏上遇到了一些问题 我无法强制将小吃栏移动到底部栏上方 这就是设计指南告诉我应该的方式https www google com design spec components bottom navigation html botto
  • 提高由整数商定义的变量的精度

    假设我有以下程序 program derp implicit none integer parameter ikind selected real kind 18 real kind ikind a 2 0 3 0 print a end
  • System.getproperty("spring.profiles.active") 在 JPA 实体监听器中总是获取 Null

    我正在尝试获取 Spring 活动配置文件JPA实体监听器使用System getproperty spring profiles active 但它总是返回空配置文件 但是我已经检查了服务器并且配置文件配置正确 我尝试使用环境获取 Spr
  • 如何更改过去的提交以包含丢失的文件?

    我已提交更改 但忘记将文件添加到更改集中 在其他提交之后 我意识到该文件现在从HEAD 4 commit 如何重写以前的提交以包含丢失的文件 我意识到人们可以谷歌并来这里找到一个更简单的答案 如果这只是最后一次提交怎么办 OP的问题是修复历
  • 如何使用 Spring Boot 和 Cassandra 将枚举持久化为序数?

    我已添加到实体的枚举字段 CassandraType type DataType Name INT 然而 在发送到 Cassandra 的语句中使用的不是枚举的序数 而是字符串表示形式 因此我收到以下错误 org springframewo