Hibernate 中的枚举,作为枚举持久存在
在我的 MySQL 数据库中,有列“gender enum('male','female')”,
我创建了枚举“com.mydomain.myapp.enums.Gender”,并在我的 Person
中实体我被定义为“性别”。
现在我想将枚举类型保留在我的 MySQL 数据库中,但是当我启动我的应用程序时,我得到:
MyApp.Person 中性别列的列类型错误。找到:枚举,预期:整数
这是为什么?这相当于我用“@Enumerated(EnumType.ORDINAL)”注释了我的“性别”,但我没有。 EnumType 似乎只能是 ORDINAL 或 STRING,那么我如何指定它应该将该字段视为枚举,而不是 int? (并不是说有太大区别,但足以让它感到不安。)
In my MySQL database, there's the column "gender enum('male','female')"
I've created my enum "com.mydomain.myapp.enums.Gender", and in my Person
entity I'm defined "Gender gender".
Now I'd want to keep the enum type in my MySQL database, but when I launch my application I get:
Wrong column type in MyApp.Person for column Gender. Found: enum, expected: integer
Why is this? This would be the equivalent as if I'd annotated my "Gender gender" with "@Enumerated(EnumType.ORDINAL)", which I haven't. EnumType seems only to be able to be either ORDINAL or STRING, so how do I specify that it should treat the field as an enum, not as an int? (not that there's much difference, but enough for it to get upset about it.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您给 Hibernate 一个列定义,它不会尝试猜测:
如果您出于任何原因不依赖 Hibernate 生成架构,您甚至不必为 columnDefinition 提供实际值。这样,您就可以删除需要保持值同步的实例。只需保持 Java 枚举和 Liquibase 或 SQL 脚本同步即可:
If you give Hibernate a column definition, it won't try to guess one:
If you aren't relying on Hibernate to generate your schema for any reason, you don't even have to provide real values for the columnDefinition. This way, you remove an instance where you need to keep the values in sync. Just keep your Java enum and your Liquibase or SQL script in sync:
我的理解是,MySQL 枚举类型是非常专有的,Hibernate 不能很好地支持,请参阅 来自 Gavin King 的评论(这个相关问题有点不同,但这不是重要的部分)。
因此,我实际上认为您必须使用自己的
UsereType
,并且我建议使用 Java 5 EnumUserType (请参阅 Appfuse 的 Java 5 Enums Persistence with Hibernate 为例)。就我个人而言,我只是忘记了使用 MySQL 枚举的想法,我不相信“好处”值得(参见 此答案了解更多详细信息)。
My understanding is that MySQL enum type is very proprietary and not well supported by Hibernate, see this comment from Gavin King (this related issue is a bit different but that's not the important part).
So, I actually think that you'll have to use your own
UsereType
and I'd recommend to use the Flexible solution - working version from the Java 5 EnumUserType (see Appfuse's Java 5 Enums Persistence with Hibernate for an example).Personally, I'd just forget the idea to use MySQL enum, I'm not convinced that the "benefits" are worth it (see this answer for more details).
尝试使用 @Enumerated(EnumType.STRING) 并像这样定义枚举,
注意小写值。
这至少适用于 VARCHAR 列。它将枚举存储为字符串“male”或“female”。
Try to use @Enumerated(EnumType.STRING) and define your enum like this
Note lower case values.
This will work at least with VARCHAR columns. It will store enum as string 'male' or 'female'.
不知道为什么它不在 Hibernate 文档中
但你可以这样做
not sure why it is not in Hibernate documentation
but you can do this
不适用于这种情况,但如果有人使用 XML 映射:
数据库列类型必须是数字,例如 mysql 上的tinyint,以便可以读取序数值。
Not for this case but if someone is using XML mapping:
The database column type must be numeric e.g. tinyint on a mysql to make reading ordinal values possible.