In this post, i want to share about storing Enum into Database (Reddis, NoSQL,...) as String then load it again as Enum type.
In Ecomerce Application, we use NoSQL repository to do CRUD with DB. Behind the sense, it use some json library(ex: Jackson) to parse your object to JSON and store to DB. With java objects, it work well but for enum type, we may lose old data after we change eum property.
Example we have enum:
and it is used in Person class:
If we persist person object to ivy repo, the JSON after serialization will look like this:
This JSON just type String so when we load this from persistence, it will be deserialized and we can get back the correct type enum for property status.
Just assume that after several months, customer want to change this enum, he want to rename the property MARRIED to GET_MARRIED, So for all data was persisted after that time it’s fine, but not the old one. we lost all data about status because system does not know that MARRIED now become GET_MARRIED. In order to keep old data we need to implement a adapter method for conversion those enum OR in other way, just use the String key/code of that enum insteadt of using directly it in Data model. The both ways is not so good in OOP perspective. The problem/risk of Add/Edit/Delete property in enum must be taken care by itself, not by the one who use it.
Step 1: Use @JsonValue & @JsonCreator suppored by Jackson library
Step 2: Use our helper to get enum and support backward Compatibility.
Now enum look like this:
After applying it, the JSON string from DB will look like this:
In other case when customer change enum from MARRIED to GET_MARRIED, the enum will be:
In Ecomerce Application, we use NoSQL repository to do CRUD with DB. Behind the sense, it use some json library(ex: Jackson) to parse your object to JSON and store to DB. With java objects, it work well but for enum type, we may lose old data after we change eum property.
Example we have enum:
and it is used in Person class:
If we persist person object to ivy repo, the JSON after serialization will look like this:
This JSON just type String so when we load this from persistence, it will be deserialized and we can get back the correct type enum for property status.
Just assume that after several months, customer want to change this enum, he want to rename the property MARRIED to GET_MARRIED, So for all data was persisted after that time it’s fine, but not the old one. we lost all data about status because system does not know that MARRIED now become GET_MARRIED. In order to keep old data we need to implement a adapter method for conversion those enum OR in other way, just use the String key/code of that enum insteadt of using directly it in Data model. The both ways is not so good in OOP perspective. The problem/risk of Add/Edit/Delete property in enum must be taken care by itself, not by the one who use it.
Solution
Now we remove that risk inside enum itself by two steps:Step 1: Use @JsonValue & @JsonCreator suppored by Jackson library
Step 2: Use our helper to get enum and support backward Compatibility.
Now enum look like this:
After applying it, the JSON string from DB will look like this:
In other case when customer change enum from MARRIED to GET_MARRIED, the enum will be:
Note that: in static block, we can do any conversion to adapt with case edit/delete/add enum or its key. just implement there and other place where it’s being used won’t need to change anything.
Here is class PersistenceEnumHelper
Hope it's helpful
Nhận xét
Đăng nhận xét