Chuyển đến nội dung chính

Storing Enum like String

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.

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

Bài đăng phổ biến từ blog này

How to ensure double write consistency between the cache and the database?

Interview question How to ensure double write consistency between the cache and the database? Interviewer psychoanalysis You only need to use the cache, it may involve double storage and double storage of the database. As long as you double write, there will be data consistency problems. How do you solve the consistency problem? Analysis of interview questions In general, if you allow the cache to be slightly inconsistent with the database, that is, if your system  is not strictly required to  "cache + database" must be consistent, it is best not to do this, namely:  read request Serialize the write request  and string it into a  memory queue  . Serialization guarantees that there will be no inconsistencies, but it will also result in a significant reduction in system throughput, supporting a request on the line with machines that are several times larger than normal. Cache Aside Pattern The most classic cache + database read and write mode...

Front-end technical trend observation 2019

Top GitHub projects This is trend of front-end technology that may helpful for you. Experience Technology myliang / x-spreadsheet JS implementation of the class Excel form, it is very convenient to embed your own project nosir / cleave.js Automatic formatting<input />Input, easy to deal with credit card input, mobile phone number input, date input and other scenarios didi / chameleon Didi's cross-end development framework, class VUE syntax, multi-terminal rendering transloadit / uppy Functional Web file Upload components, modular plug-in, for modern front-end system instantpage / instant.page When the mouse hovers over the link, the preload page starts and opens directly in seconds after clicking. klaussinani / qoa Simple and easy-to-use lightweight interactive command prompt, which is a must leon-ai / leon Open source AI dialogue robot, you can write your own modules t...