」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 在映射到MySQL枚舉列時,如何確保冬眠保留值?

在映射到MySQL枚舉列時,如何確保冬眠保留值?

發佈於2025-02-27
瀏覽:884

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