If I understand you correctly, for an enum like:
public enum Color { RED, GREEN, BLUE; }
you would have 3 true/false columns in a database, one for each possible enum value. Then an EnumSet
containing, say, RED and BLUE, should be mapped to:
RED GREEN BLUE
true false true
If that's the case, the only way I know of is to write your own implementation of org.hibernate.usertype.UserType
. It's a pretty straight-forward task, with some examples available on Hibernate site and, for instance, here.
Edit: I just realized things would have to be somewhat more complex. If you wish to have one Hibernate type mapping for all possible EnumSet
in your application, you will have to do the following:
- Use
org.hibernate.usertype.ParameterizedType
and make user type parameterizable by an enum class.
- Depending on values of enum class, determine how many and what columns to read/write.
- Instantiate the type each time an
EnumSet
is used.
- Consider the possibility of mapping multiple sets in one table, as well as how adding a new enum value will influence workings of existing type.
This could easily amount to full day's work, but seems quite doable. Hope you figure it out from here.