I am trying to convert the text field in a giant Postgres table which has the values like (1, 2, 3, 4, __ALL) into an enum. The plan is to update the text field to an enum in the database.
I understand that I can't do Enum as follows in Java;
public enum MyEnum {
1,2,3,4;
}
I can either do as;
public enum MyEnum {
_1,_2,_3,_4;
}
Or:
public enum QuartileNumber {
ONE("1"), TWO("2"), THREE("3"), FOUR("4"), __ALL("__ALL");
}
My hibernate mapping looks like this;
<property name="quartileNumber" column="quartile_number">
<type name="com.altosresearch.model.PostgreSQLEnumType">
<param name="enumClass">com.altosresearch.model.QuartileNumber</param>
<param name="type">12</param>
</type>
</property>
Using this mapping and my Java enum (QuartileNumber);
QuartileNumbers.valueOf("__ALL") - returns '_ALL' (the expected enumClass of com.altosresearch.model.QuartileNumber)
QuartileNumbers.valueOf("ONE") - returns 'ONE' (which will be obviously rejected in the database as it doesn't match one of the DB enum values (1, 2, 3, 4, __ALL).
So, I am able to insert correctly only the value '__ALL' into the table. Need suggestions to insert values 1, 2, 3, 4 as well here. Thanks!
Note: Trying to use the same existing values in the database ('1','2','3','4','__ALL') to avoid the code changes.
What can help here is to use the AttributeConverter for the Enum.
Here is a link from Thorben Janssen https://thoughts-on-java.org/jpa-21-type-converter-better-way-to/
how to save in the DB values : 1,2,3,4,__ALL from the Enum definition:
public enum QuartileNumber {
ONE("1"), TWO("2"), THREE("3"), FOUR("4"), __ALL("__ALL");
}