英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论