Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...