Menelaos Bakopoulos Menelaos Bakopoulos - 3 years ago 148
Java Question

Dealing with changed ENUM definitions - database

Introduction



The lead architect went and changed the ENUM definition in a spring boot project.

From:

public enum ProcessState{
C("COMPLETE"), P("PARTIAL");
}


To:

public enum ProcessState{
COMPLETE("COMPLETE"), PARTIAL("PARTIAL");
}


What is the proper way to deal with this? Some other
Java
Spring Boot
applications are now breaking. Would there be a way to tell the jackson deserializer to perform some kind of conversion in these situations?

My Current Work-Around



What I did was to run two update statements on the oracle database:

UPDATE store set PAYLOAD = REPLACE(PAYLOAD, '"processState":"P"','"processState":"PARTIAL"') where PAYLOAD like '%"processState":"P"%';

UPDATE store set PAYLOAD = REPLACE(PAYLOAD, '"processState":"C"','"processState":"COMPLETE"') where PAYLOAD like '%"processState":"C"%';


Question



So are there other ways? Could I do it by adding some deserialization/conversion code somewhere for these specific cases? Is there a more elegant way than running a replace SQL statement?

Could I do some kind of hack on a specific java sub-package, and say "use this enum instead of that enum..." or use one of the two? But without affecting the rest of the code?

The error:



java.lang.IllegalArgumentException: No enum constant

Answer Source

Ideally we store value of emum rather than Enum.
So, you should save ENUM values like COMPLETE,PARTIAL
For JSON serialization and de-serialization, use @JsonValue

@JsonValue
public String toValue() {
   return value;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download