Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a set of enum type in Hibernate?

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

like image 535
Cuga Avatar asked Jul 14 '09 15:07

Cuga


1 Answers

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>
like image 114
Mike Avatar answered Sep 19 '22 09:09

Mike