Java Hibernate 中的枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/417062/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:23:39  来源:igfitidea点击:

Enumerations in Hibernate

javahibernatedao

提问by Georgios Gousios

It is often useful to have a field in a DAO whose value comes from a Java enumeration. A typical example is a login DAO where you usually have a field that characterises the user as "NORMAL" or "ADMIN". In Hibernate, I would use the following 2 objects to represent this relationship in a (semi-)typesafe way:

在 DAO 中有一个字段的值来自 Java 枚举通常很有用。一个典型的例子是登录 DAO,其中通常有一个字段将用户描述为“NORMAL”或“ADMIN”。在 Hibernate 中,我将使用以下 2 个对象以(半)类型安全的方式表示这种关系:

class User {
    String username;
    String passwd;
    UserType type;
}

class UserType {
    private enum Type {ADMIN, NORMAL};
    private String type;

    //Setters/Getters for Hibernate
    public void setType(String type);
    public String getType();

    //Setters/Getters for user
    public void setUserType(UserType.Type t);
    public UserType.Type getUserType();

    public static UserType fromType(UserType.Type t);
}

This works, but I find the UserType class ungly and requiring too much bureaucracy just to store a couple of values. Ideally, Hibernate should support enum fields directly and would create an extra table to store the enumeration values.

这是有效的,但我发现 UserType 类很丑陋,并且需要太多的官僚作风来存储几个值。理想情况下,Hibernate 应该直接支持枚举字段,并会创建一个额外的表来存储枚举值。

My question is: Is there any way to directly map an enumeration class in Hibernate? If not, is my pattern for representing enumerations good enough or am I missing something? What other patterns do people use?

我的问题是:有没有办法在 Hibernate 中直接映射枚举类?如果不是,我代表枚举的模式是否足够好或者我错过了什么?人们还使用哪些其他模式?

采纳答案by Gareth Davis

using hibernate or JPA annotations:

使用休眠或 JPA 注释:

class User {
   @Enumerated(EnumType.STRING)
   UserType type
}

UserType is just a standard java 5 enum.

UserType 只是一个标准的 java 5 枚举。

I can't imagine this is just limited to just annotations but I don't actually know how to do this with hbm files. It may be very version dependant, I'm guessing but I'm pretty sure that hibernate 3.2+ is required.

我无法想象这仅限于注释,但我实际上不知道如何使用 hbm 文件来做到这一点。它可能非常依赖于版本,我猜但我很确定需要 hibernate 3.2+。

edit: it is possible in a hbm, but is a little messy, have a look at this forum thread

编辑:在 hbm 中是可能的,但有点乱,看看这个论坛帖子

回答by Craig

From the Hibernate documentation: http://www.hibernate.org/272.html

来自 Hibernate 文档:http: //www.hibernate.org/272.html

You can create a new typedef for each of your enums and reference the typedefs in the property tag.

您可以为每个枚举创建一个新的 typedef,并在 property 标记中引用 typedef。

Example Mapping - inline <type>tag

示例映射 - 内联<type>标记

  <property name='suit'>
    <type name="EnumUserType">
      <param name="enumClassName">com.company.project.Suit</param>
    </type>
  </property>

Example Mapping - using <typedef>

示例映射 - 使用 <typedef>

  <typedef name="suit" class='EnumUserType'>
      <param name="enumClassName">com.company.project.Suit</param>
  </typedef>

  <class ...>
    <property name='suit' type='suit'/>
  </class>