可以Java Cassandra 4.x映射器将枚举转换为整数吗?

huangapple go评论84阅读模式
英文:

Can the Java Cassandra 4.x mapper convert an Enum to an int?

问题

我有一个非常简单的枚举

public enum Axis {
  X, Y, Z;
}

我过去常常使用 EnumOrdinalCodec 3.x 驱动程序将其保存为数据库中的 int。但是在 4.x 中,我遇到了一个错误
> CQL ks.table: [snip] 在实体类中定义:[snip] 声明了编解码器注册表不支持的类型映射:
字段:axis,实体类型:[snip].Axis,CQL 类型:INT

我找不到任何可以用来告诉驱动程序如何映射枚举的注释。唯一的钩子似乎是 MapperResultProducer,我无法理解也找不到示例。

除了手动将枚举映射到其序数之外,是否有更好的方法?

英文:

I've got a really simple enum

public enum Axis {
  X, Y, Z;
}

I used to use the EnumOrdinalCodec 3.x driver to save this into an int in the database. With 4.x I get an error
> The CQL ks.table: [snip] defined in the entity class: [snip] declares type mappings that are not supported by the codec registry:
Field: axis, Entity Type: [snip].Axis, CQL type: INT

I can't find any annotations that can be used to tell the driver how to map the enum. The only hook appears to be MapperResultProducer which I cannot comprehend nor find examples of.

Is there a better way than manually mapping the enum to its ordinal?

答案1

得分: 1

This functionality was restored starting with Java driver 4.8 - there is a new ExtraTypeCodecs class that contains additional codecs. You'll need to use codec returned by function ExtraTypeCodecs.enumOrdinalsOf(Class). (but see the comment there, that it's better not to use that because number can change if you change your enum incorrectly)

英文:

This functionality was restored starting with Java driver 4.8 - there is a new ExtraTypeCodecs class that contains additional codecs. You'll need to use codec returned by function ExtraTypeCodecs.enumOrdinalsOf(Class). (but see the comment there, that it's better not to use that because number can change if you change your enum incorrectly)

huangapple
  • 本文由 发表于 2020年9月22日 18:48:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64008139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定