Hibernate 中的枚举,作为枚举持久存在

发布于 2024-08-19 17:59:13 字数 418 浏览 5 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

羁〃客ぐ 2024-08-26 17:59:13

如果您给 Hibernate 一个列定义,它不会尝试猜测:

@Column(columnDefinition = "enum('MALE','FEMALE')")
@Enumerated(EnumType.STRING)
private Gender gender;

如果您出于任何原因不依赖 Hibernate 生成架构,您甚至不必为 columnDefinition 提供实际值。这样,您就可以删除需要保持值同步的实例。只需保持 Java 枚举和 Liquibase 或 SQL 脚本同步即可:

@Column(columnDefinition = "enum('DUMMY')")
@Enumerated(EnumType.STRING)
private ManyValuedEnum manyValuedEnum;

If you give Hibernate a column definition, it won't try to guess one:

@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:

@Column(columnDefinition = "enum('DUMMY')")
@Enumerated(EnumType.STRING)
private ManyValuedEnum manyValuedEnum;
浊酒尽余欢 2024-08-26 17:59:13

我的理解是,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).

寻找一个思念的角度 2024-08-26 17:59:13

尝试使用 @Enumerated(EnumType.STRING) 并像这样定义枚举,

enum Gender {
  male,
  female
}

注意小写值。

这至少适用于 VARCHAR 列。它将枚举存储为字符串“male”或“female”。

Try to use @Enumerated(EnumType.STRING) and define your enum like this

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'.

执手闯天涯 2024-08-26 17:59:13

不知道为什么它不在 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>

not sure why it is not in Hibernate documentation
but you can do this

<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>
半﹌身腐败 2024-08-26 17:59:13

不适用于这种情况,但如果有人使用 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>

数据库列类型必须是数字,例如 mysql 上的tinyint,以便可以读取序数值。

Not for this case but if someone is using XML mapping:

<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文