How
Hope's approach to better handling Enum (lists)
Enum handling can be a tricky issue. In applications, enum constants are great for avoiding hard-coding, but how do you handle enums during API transmission and serialization/deserialization at the database layer?
The complexity increases further if list support is added. Enums have two default identifiers: name
and ordinal
.
While ordinal
seems to be a very efficient way to pass and store information, it can be compromised during the evolution of the enum and is harder to understand and read.
Given this, the Hope
framework, drawing on extensive engineering practices, has found a fairly moderate solution to the problem of handling the Enum
data type:
Two interfaces:
Two formats:
Goals:
Since 1.1.5-RELEASE, only two configuration methods remain, see also jakarta.persistence.EnumType:
hope.common.enumeration.Enumeration#title()
, delegates to java.lang.Enum#name()
.hope.common.enumeration.Enumeration#code()
does not use ordinal
(no longer used within the Hope framework to avoid explanation and cognitive costs).enum EnumType {
//Default as STRING
// Persist `hope.common.enumeration.Enumeration#title()` usually this will delegate to the `java.lang.Enum#name()`
STRING = 0;
// Persist `hope.common.enumeration.Enumeration#code()`
CODE = 1;
}
Entity
definition:
status_list
: List of OrderStatusEnum
, example with CODE
: 100001,10002
.status
: Single OrderStatusEnum
, example with TITLE
: PLACED
.repeated OrderStatusEnum status_list = 3 [(hope.persistence.column) = {
name: "STATUS_LIST",
type: VARCHAR,
length:{
value: 64
},
enum_type: CODE
}];
OrderStatusEnum status = 4 [(hope.persistence.column) = {
name: "status",
type: VARCHAR,
length:{
value: 64
},
enum_type: STRING
}];
Generation mainly involves Spring JDBC reader/writer (OLAP), Mybatis Reader/Query (OLTP);
Hope OLTP is based on Spring Data JDBC (not JPA/Hibernate/Mybatis)!
OLAP is based on Mybatis Query DSL, used for statistics and queries, not recommended for write operations!
Class Name | Function | Included Classes | Notes |
---|---|---|---|
*domain.bootstrap.converter.{Domain}Converter | Spring data convert | OrderStatus(CodeListReaderConverter, CodeListWriterConverter, Title) | Injected during Spring JDBC initialization |
*domain.bootstrap.mybatis.type.*TypeHandler | iBatis type TypeHandler | OrderStatus(CodeListTypeHandler, TitleListTypeHandler, Title) | Batis Column Converter |
The Hope framework will:
JdbcCustomConversions
.org.mybatis.dynamic.sql.SqlColumn
with TypeHandler
and parameter ParameterTypeConverter
.