如何在Hibernate中映射一组枚举类型? [英] How to map a set of enum type in Hibernate?

查看:89
本文介绍了如何在Hibernate中映射一组枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在hibernate中,是否可以为一个类定义映射到一组枚举?

我已经能够找到如何定义集合映射的示例,并且我已经能够找到单独的示例来说明如何映射枚举,但是我无法确定了解如何定义一个类的枚举。



任何人都可以给我一个例子吗?

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

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

  + ------------- --- + + ------------ + + ------------ + 
| Wicket | |参考表| | WicketType |
+ ---------------- + + ------------ + + ------------ +
| INT | W_ID | | | | W_TypeId |
| .... | | FK | W_ID | FK | WicketType |
| INT | TYPE | ---- | W_TypeId | ---- | |
+ ---------------- + + ------------ + + ------------ +

再次感谢

解决方案

这个

a>没有做你需要的东西?



为了详细说明翻转初始响应,引用提供了使用枚举的序数来枚举枚举的方法。



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

  package test; 

public class WicketTypeState extends IntEnumUserType< WicketType> {
私人WicketType wicketType;

public WicketTypeState(){
//我们必须将枚举值赋给父类。
super(WicketType.class,WicketType.values());
}

public WicketType getWicketType(){
return wicketType;
}

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






定义枚举表的映射:

 < hibernate-mapping package =test> 
< class name =Wickettable =Wicket>
< id name =idcolumn =ID/>
< set name =wicketTypestable =WicketTypeinverse =true>
< key column =ID/>
< / set>
< / class>
< / hibernate-mapping>

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

 < hibernate-mapping package =test> 
< class name =WicketTypeStatelazy =truetable =WicketType>
< id name =WicketType
type =test.WicketTypeState/>
< / class>
< / hibernate-mapping>

这个工作在我的盒子(tm)上,让我知道你是否需要更多的信息。


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         |    | Ref Table  |    | WicketType |
+----------------+    +------------+    +------------+
| INT     | W_ID |    |            |    | W_TypeId   |
| ....    |      | FK | W_ID       | FK | WicketType |
| INT     | TYPE |----| W_TypeId   |----|            |
+----------------+    +------------+    +------------+

Thanks again

解决方案

Does this not 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.

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.

这篇关于如何在Hibernate中映射一组枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆