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

Map PSQL Enum-Array to EnumSet #126

Open jwgmeligmeyling opened 5 years ago

jwgmeligmeyling commented 5 years ago

I've seen some request before to be able to map an EnumSet to a column in Hibernate (for example: https://developer.jboss.org/thread/106544). Currently can map an enum array to a Enum[] value, but arrays are not as convenient as being able to use the Java Collection API. One downside of this approach is that an EnumSet has set characteristics, wheras an array does not. So it might make more sense to map it to a List instead. Perhaps we could add support both.

Anyways, currently the issue is easily worked around by wrapping and unwrapping the value in the attribute getter and setter. However, it might be a nice inclusion as custom type.

I have quickly played with the idea and it seems we can easily pull this off, somewhat relying on the implementation for EnumArrayType. However, there are a few downsides. Because the generic type cannot (supposedly) be determined from the DynamicParameterizedType parameter values, the enum value class would have to be specified through the @Type's parameters.

@vladmihalcea do you have interest in including this as a feature? And if so, how would you like to see the general structure of such an addition?

I suppose that the type implementation has to be separated from the entire type.array package, even though we're dealing with DB arrays, we're not dealing with Java arrays, which is what this package is for. So then I'd personally put it in type.basic. Also, we might want to setup a type composition structure if we want to support mapping to any of Collection, Set, List, etc as well.

Given a few pointers I can spend some time on implementing this in the foreseeable future.

vladmihalcea commented 5 years ago

Sounds like a goid idea. We can start with the RegularEnumSet and map it to a bigint.

jwgmeligmeyling commented 4 years ago

This seems easier to do with the restructuring of the array types in https://github.com/vladmihalcea/hibernate-types/issues/97

vladmihalcea commented 4 years ago

I think it's worth investigating, indeed.