java 如何在 Hibernate 中映射一组枚举类型?

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

How to map a set of enum type in Hibernate?

javahibernateormnhibernate-mapping

提问by Cuga

In hibernate, is it possible to define a mapping for a class to a set of enums?

在休眠中,是否可以定义类到一组枚举的映射?

I've been able to find examples of how to define mappings of Sets and I've been able to find separate examples for how to map Enums, but I cannot figure out how to define a of Enums for a class.

我已经能够找到有关如何定义集合映射的示例,并且已经能够找到有关如何映射枚举的单独示例,但我无法弄清楚如何为类定义枚举。

Could anyone please provide me with an example?

谁能给我一个例子?

This is being built on top of an existing application, so I cannot alter the database schema.

这是建立在现有应用程序之上的,因此我无法更改数据库架构。

This is the relation I wish to model. Wicket is a normal class and WicketType is a Java Enum.

这是我希望建模的关系。Wicket 是一个普通的类,而 WicketType 是一个 Java Enum。

+----------------+    +------------+    +------------+
| Wicket         |    | Ref Table  |    | WicketType |
+----------------+    +------------+    +------------+
| INT     | W_ID |    |            |    | W_TypeId   |
| ....    |      | FK | W_ID       | FK | WicketType |
| INT     | TYPE |----| W_TypeId   |----|            |
+----------------+    +------------+    +------------+

Thanks again

再次感谢

采纳答案by Rich Seller

Does thisnot do what you need?

难道不是你所需要的?

To elaborate on the flippant initial response, the reference provides a means to use the ordinal of the enum to map enumerations.

为了详细说明轻率的初始响应,参考提供了一种使用枚举的序数来映射枚举的方法。

In this case it's actually simpler than it looks, because you are hosting the enums in a set, you need to provide an accessor for the WicketType to the sub-type of IntEnumUserType, the super-type will take care of mapping the ordinal to the instance.

在这种情况下,它实际上比看起来更简单,因为您将枚举托管在一个集合中,您需要为 IntEnumUserType 的子类型提供 WicketType 的访问器,超类型将负责将序数映射到实例。

package test;

public class WicketTypeState extends IntEnumUserType<WicketType> {
    private WicketType wicketType;

public WicketTypeState() {
    // we must give the values of the enum to the parent.
    super(WicketType.class, WicketType.values());
}

    public WicketType getWicketType() {
        return wicketType;
    }

    public void setWicketType(final WicketType wicketType) {
        this.wicketType = wicketType;
    }
}

Define the mappings for the enum table:

定义枚举表的映射:

<hibernate-mapping package="test">  
  <class name="Wicket" table="Wicket">
    <id name="id" column="ID"/>
    <set name="wicketTypes" table="WicketType" inverse="true">
      <key column="ID"/>
      <one-to-many class="test.WicketTypeState"/>
    </set>
  </class>
</hibernate-mapping>

Then for the type with the set of enums, define a set mapping for that property:

然后对于具有枚举集的类型,为该属性定义一个集合映射:

<hibernate-mapping package="test">
  <class name="WicketTypeState" lazy="true" table="WicketType">
    <id name="WicketType" 
      type="test.WicketTypeState"/>
  </class>
</hibernate-mapping>

This worked on my box(tm), let me know if you need any more info.

这适用于我的盒子(tm),如果您需要更多信息,请告诉我。

回答by Mike

A simpler way is

一个更简单的方法是

<typedef name="WicketTypeType" class="org.hibernate.type.EnumType">
  <param name="enumClass">Wicket</param>
  <param name="type">12</param>
</typedef>

<class  name="Wicket"...

    <set name="WicketType" table="Ref Table">
        <key column="W_ID" />
        <element column="W_TypeID" type="WicketTypeType"/>
    </set>

...
</class>

回答by Venkat

The sample code below shows how what you want can be achieved with annotations.

下面的示例代码显示了如何使用注释实现您想要的。

@Entity
@Table (name = "wicket")
public class Wicket {

    ...
    ...

    private List<WicketType> wicketTypes = new ArrayList<WicketType>();

    @CollectionOfElements(fetch=FetchType.EAGER)
    @JoinTable(
            name="wicket_wicket_types", // ref table.
            joinColumns = {@JoinColumn(name="wicket_id")}
    )
    @Column(name="wicket_type_id")
    public List<WicketType> getWicketTypes() {
        return this.wicketTypes;
    }

    public void setWicketTypes(List<WicketType> wicketTypes) {
        this.wicketTypes = wicketTypes;
    }
    ...
    ...
}

WicketTypeis a standard Java 5 Enum whose order and ordinals of enum declarations match order and the column (wicket_type_id) values in the wicket_typetable.

WicketType是一个标准的 Java 5 枚举,其枚举声明的顺序和序号与表中的顺序和列 ( wicket_type_id) 值匹配wicket_type

public enum WicketType {
   WICKET_TYPE1, WICKET_TYPE2 ...
}