Java Hibernate 中的枚举,作为枚举持久化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2160700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enum in Hibernate, persisting as an enum
提问by niklassaers
In my MySQL database, there's the column "gender enum('male','female')"
在我的 MySQL 数据库中,有一列“gender enum('male','female')”
I've created my enum "com.mydomain.myapp.enums.Gender", and in my Person
entity I'm defined "Gender gender".
我已经创建了我的枚举“com.mydomain.myapp.enums.Gender”,并且在我的Person
实体中我定义了“性别”。
Now I'd want to keep the enum type in my MySQL database, but when I launch my application I get:
现在我想在我的 MySQL 数据库中保留枚举类型,但是当我启动我的应用程序时,我得到:
Wrong column type in MyApp.Person for column Gender. Found: enum, expected: integer
MyApp.Person 中列性别的列类型错误。找到:枚举,预期:整数
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.)
为什么是这样?这相当于我用“@Enumerated(EnumType.ORDINAL)”注释了我的“性别”,而我没有。EnumType 似乎只能是 ORDINAL 或 STRING,那么我如何指定它应该将该字段视为枚举,而不是 int?(并不是说差别太大,但足以让它为此感到不安。)
采纳答案by Pascal Thivent
My understanding is that MySQL enum type is very proprietary and not well supported by Hibernate, see this commentfrom Gavin King (this related issue is a bit different but that's not the important part).
我的理解是 MySQL 枚举类型是非常专有的,Hibernate 并没有很好地支持,请参阅Gavin King 的评论(这个相关问题有点不同,但这不是重要的部分)。
So, I actually think that you'll have to use your own UsereType
and I'd recommend to use the Flexible solution - working versionfrom the Java 5 EnumUserType(see Appfuse's Java 5 Enums Persistence with Hibernatefor an example).
所以,我倒认为你必须用你自己的UsereType
,我会建议使用灵活的解决方案-工作版本从Java 5的EnumUserType(见AppFuse的Java 5的枚举持久性与Hibernate的例子)。
Personally, I'd just forget the idea to use MySQL enum, I'm not convinced that the "benefits" are worth it (see this answerfor more details).
就个人而言,我只是忘记了使用 MySQL 枚举的想法,我不相信“好处”值得(有关更多详细信息,请参阅此答案)。
回答by Juha Syrj?l?
Try to use @Enumerated(EnumType.STRING) and define your enum like this
尝试使用 @Enumerated(EnumType.STRING) 并像这样定义您的枚举
enum Gender {
male,
female
}
Note lower case values.
注意小写值。
This will work at least with VARCHAR columns. It will store enum as string 'male' or 'female'.
这至少适用于 VARCHAR 列。它将枚举存储为字符串“男性”或“女性”。
回答by Jimmie Fulton
If you give Hibernate a column definition, it won't try to guess one:
如果你给 Hibernate 一个列定义,它不会尝试猜测:
@Column(columnDefinition = "enum('MALE','FEMALE')")
@Enumerated(EnumType.STRING)
private Gender gender;
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:
如果您出于任何原因不依赖 Hibernate 生成架构,则您甚至不必为 columnDefinition 提供实际值。通过这种方式,您可以删除需要保持值同步的实例。只需保持 Java 枚举和 Liquibase 或 SQL 脚本同步即可:
@Column(columnDefinition = "enum('DUMMY')")
@Enumerated(EnumType.STRING)
private ManyValuedEnum manyValuedEnum;
回答by Christof Aenderl
Not for this case but if someone is using XML mapping:
不适用于这种情况,但如果有人使用 XML 映射:
<property name="state" column="state" not-null="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">com.myorg.persistence.data.State</param>
</type>
</property>
The database column type must be numeric e.g. tinyint on a mysql to make reading ordinal values possible.
数据库列类型必须是数字,例如 mysql 上的 tinyint 以使读取序数值成为可能。
回答by Kikoz
not sure why it is not in Hibernate documentation but you can do this
不知道为什么它不在 Hibernate 文档中,但您可以这样做
<property name="type" column="type" not-null="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">com.a.b.MyEnum</param>
<param name="type">12</param>
<!-- 12 is java.sql.Types.VARCHAR -->
</type>
</property>