”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 在映射到MySQL枚举列时,如何确保冬眠保留值?

在映射到MySQL枚举列时,如何确保冬眠保留值?

发布于2025-02-27
浏览:696

How to Ensure Hibernate Preserves Enum Values When Mapping to a MySQL Enum Column? 
在Hibernate中保存枚举值:故障排除错误的列类型

在数据持久性领域,确保数据模型,数据库schemas及其尊重的映射之间的相适合性。在Java中使用枚举类型时,至关重要的是要确定如何将这些枚举映射到基础数据库中。

在您的情况下,您将MySQL列定义为枚举并在Java代码中创建相应的枚举。但是,您遇到以下错误:“ MyApp中的错误列类型。针对列性别。找到:枚举,预期:整数。”当Hibernate期望性别列是一个整数时,由于枚举被视为序数或字符串而产生的错误,这取决于@enumerereated注释的规范。可以解决此问题,您可以使用columndefinition Attribute:

By providing a column definition, you instruct Hibernate not to guess the column type but to adhere to the specified definition.

Alternatively, if you're not using Hibernate to generate your schema, you can remove the need for column definition values by setting them to arbitrary values:

@Column(columnDefinition = “枚举('虚拟')”) @enumerated(EnumType.String) 私有ManyValueDenum ManyValueDenum;

以这种方式,您可以确保将枚举值保留在您的Java枚举中,同时同步您的liquibase或sql脚本。
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3