在 HBM 中添加枚举作为类属性

发布于 2024-08-15 12:17:23 字数 380 浏览 7 评论 0原文

我正在尝试在 HBM 文件中创建一个类,其中包含一个枚举作为字段。

HBM 与此类似:

<class name="a.b.c.myObject" table="OBJECT" >
       <property name="myEnum" column="EXAMPLE" type="a.b.c.myEnum" />
</class>

假设这是枚举:

public enum myEnum{
    a, b, c;
}

问题是在数据库中我期望看到该枚举的字符串值(a、b 或 c),但我得到了该字段的原始数据。

我该如何解决这个问题?

I am trying to create a class in HBM file which contains an Enum as a field.

The HBM is similar to this:

<class name="a.b.c.myObject" table="OBJECT" >
       <property name="myEnum" column="EXAMPLE" type="a.b.c.myEnum" />
</class>

and let's say that this is the Enum:

public enum myEnum{
    a, b, c;
}

The problem is that in the DB I expected to see the String value of that enum (a,b or c) but instead I got the raw data of that field.

How can I solve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

风为裳 2024-08-22 12:17:23

这是 Hibernate 3.6.x 的解决方案:

<class name="a.b.c.myObject" table="OBJECT">
  <property name="myEnum" column="EXAMPLE">
    <type name="org.hibernate.type.EnumType">
      <param name="enumClass">a.b.c.myEnum</param>
    </type>       
  </property>
</class>

Here is the solution with Hibernate 3.6.x :

<class name="a.b.c.myObject" table="OBJECT">
  <property name="myEnum" column="EXAMPLE">
    <type name="org.hibernate.type.EnumType">
      <param name="enumClass">a.b.c.myEnum</param>
    </type>       
  </property>
</class>
吻风 2024-08-22 12:17:23

类似于 @monim 答案,但更优雅的方式:

<class name="a.b.c.myObject" table="OBJECT">
    <property name="myEnum" column="EXAMPLE">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">a.b.c.myEnum</param>
            <param name="useNamed">true</param>
        </type>       
    </property>
</class>

Similar to @monim answer, but more elegant way:

<class name="a.b.c.myObject" table="OBJECT">
    <property name="myEnum" column="EXAMPLE">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">a.b.c.myEnum</param>
            <param name="useNamed">true</param>
        </type>       
    </property>
</class>
此刻的回忆 2024-08-22 12:17:23

1) 简单的解决方案:使用 Hibernate Annotations 而不是基于 XML 的映射。枚举支持是内置的< /a>:

@Entity
public class MyObject {
  @Enumerated(EnumType.STRING)
  @Column(name="EXAMPLE")
  private MyEnum myEnum;
}

2) 如果您不能使用注释,您仍然可以使用它们在基于 XML 的映射中提供的 EnumType。在部署期间,您确实需要在类路径中包含适当的 hibernate-annotations.jar 但不存在编译时依赖项:

<class name="a.b.c.myObject" table="OBJECT" >
  <property name="myEnum" column="EXAMPLE" type="org.hibernate.type.EnumType"/>
</class>

1) Easy solution: use Hibernate Annotations instead of XML-based mappings. Enum support is built-in:

@Entity
public class MyObject {
  @Enumerated(EnumType.STRING)
  @Column(name="EXAMPLE")
  private MyEnum myEnum;
}

2) If you can't use annotations, you can still use the EnumType they provide in XML-based mappings. You do need to have appropriate hibernate-annotations.jar in your classpath during deployment but there's no compile-time dependency:

<class name="a.b.c.myObject" table="OBJECT" >
  <property name="myEnum" column="EXAMPLE" type="org.hibernate.type.EnumType"/>
</class>
书间行客 2024-08-22 12:17:23

只需编辑@Emmanuel Bourg答案并添加另一个,如下所示:

<class name="a.b.c.myObject" table="OBJECT">
    <property name="myEnum" column="EXAMPLE">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">a.b.c.myEnum</param>
            <param name="type">12</param>
        </type>       
    </property>
</class>

12相当于java.sql.Types.VARCHAR

Just editing @Emmanuel Bourg answer and adding another <param> like this:

<class name="a.b.c.myObject" table="OBJECT">
    <property name="myEnum" column="EXAMPLE">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">a.b.c.myEnum</param>
            <param name="type">12</param>
        </type>       
    </property>
</class>

12 is equivilant to java.sql.Types.VARCHAR

浊酒尽余欢 2024-08-22 12:17:23

由于一些尚不清楚的原因,Hibernate 开发人员坚持保持 Hibernate 核心与 Java5 之前的版本兼容,这意味着不支持枚举。当您尝试保留枚举字段时,它只是将其序列化,然后您将获得二进制数据。

如果您想使用 .hbm 映射配置保留枚举,则必须为要处理的每个枚举类型创建并配置自定义 UserType,这是乏味且令人恼火的。 Hibernate 文档 wiki 有大量示例,其中许多示例似乎相互矛盾,还有一些示例其中甚至有效。

但是,如果您使用 Hibernate 注释,您将获得完整的 java5 支持,包括自动处理 java5 枚举。你必须做其中之一,这真是太遗憾了。

For reasons that remain obscure, the Hibernate developers insist on keeping the Hibernate core compatible with pre-Java5, and that means no enum support. When you try to persist an Enum field, it just serializes it, and you get binary data.

If you want to persist enums using an .hbm mapping configuration, you have to create and configure a custom UserType for each enum type you want to handle, which is tedious and irritating. The Hibernate documentation wiki has plenty of examples, many of which seem to contradict each other, and some of which even work.

If you use Hibernate annotations, though, you get full java5 support, including automatic handling of java5 enums. It's just a real shame that you have to do one or the other.

指尖微凉心微凉 2024-08-22 12:17:23

您需要使用 UserType 来有效地保留该内容: https://www.hibernate.org/265.html

You need to use a UserType to persist that efficiently: https://www.hibernate.org/265.html

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