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.4k stars 366 forks source link

ListArrayType and Enums columnDefinition to text[] #364

Open ProIcons opened 2 years ago

ProIcons commented 2 years ago

When i declare the following type:

@TypeDef(
        name = "viewing_types",
        typeClass = ListArrayType.class,
        parameters = {@Parameter(name = ListArrayType.SQL_ARRAY_TYPE, value = "text")}
    )

and use it like this

@Type(type = "viewing_types")
@Column(name = VIEWING_TYPES, columnDefinition = "text[]")
@Enumerated(EnumType.STRING)
private List<ViewingType> viewingTypes;

it seems that it is completely ignoring the @Enumerated and instead it creates an array of the toString() values of the enums, instead of the name() values or ordinal() for enum types EnumType.String and EnumType.Ordinal accordingly

Even if it had as the intended behavior to be always @Enumerated(EnumType.String) It is inconsistent since by default on simple fields annotated with @Enumerated(EnumType.STRING) hibernate is using the name() method of the enum to persist the values on the database instead of the toString().

While this works on persisting the object to the database (even though it is using the toString() value), on fetching it will fail with an IllegalArgumentException which is been thrown from the Enum.valueOf() ​on https://github.com/vladmihalcea/hibernate-types/blob/d7cc02d550f6d84df4f50a79ced1b71e6ee2a2d8/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ArrayUtil.java#L191

since toString() might be overridden and not match name() any more.

vladmihalcea commented 2 years ago

This feature was never implemented, but you can implement it if you need it.

ProIcons commented 2 years ago

Since there are handlings for Enum Values, how is it exactly not implemented? I'm not sure i follow.

vladmihalcea commented 2 years ago

Check out the source code and you'll see what is implemented at the moment and why it's not working for you.