vladmihalcea / hypersistence-utils

The Hypersistence Utils library (previously known as Hibernate Types) gives you Spring and Hibernate utilities that can help you get the most out of your data access layer.
Apache License 2.0
2.39k stars 365 forks source link

Add EnumSet as bitfield support #52

Open reitzmichnicht opened 6 years ago

reitzmichnicht commented 6 years ago

Hi

Java recommends using EnumSets as bitfield or flags enums where every enum entry has its dedicated bit. Can you add an example how an EnumSet inside an entity can be mapped to an integer column instead of having a 1:n relation?

Kind regards, Michael

jwgmeligmeyling commented 6 years ago

While this optimisation is safe to do at runtime, I'm rather reserved to extend this optimisation to the DB schema. For this reason, I prefer to persist enums by their String value rather than their ordinal, or even better, the PostgreSQLEnumType provided in this repository.

vladmihalcea commented 6 years ago

EnumSet is a collection of Enum identifiers, so I think the Type is going to be useful since the default Enum can only store a single Enum value, while this one will encode multiple Enum values (e.g. toppings on a pizza).

jwgmeligmeyling commented 6 years ago

I definitely see how it would be useful, but it just seems to dangerous to me for someone to mess up the ordinals 😄 So I personally wouldn't use it. I'd rather see a native array version of the existing enum type. Just my two cents!

mressler commented 4 years ago

I would stay away from adding EnumSet as a bitfield. The values of the enum chan and will change over time, but we don't want the mapping to change. I agree with @jwgmeligmeyling in that they should be represented as Strings.

Found this in a Google search and saw that it has some recent activity - thought I'd add my thoughts!

ke4roh commented 2 years ago

See also Issue #126