[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition

Content of the problem

It is a copy of the DB table definition as it is, and since it was covered with reserved words and definitions, it was defined with _.

Problematic enum

public enum Moge {
    hoge("hoge"), fuga("fuga"), _native("native"); //native is a reserved word


Here, the query result to Jdbc was mapped using ʻEnum.valueOf, but the value that can be obtained from the DB is native, which is different from _native`, so it could not be mapped.


I made an annotation that conveys the field corresponding to the value that can be taken from the DB, and made it possible to map by processing the annotation with reflection.


public @interface MapByValue {
    String value();


public enum Moge {
    hoge("hoge"), fuga("fuga"),
    @MapByValue(value = "native") _native("native"); //native is a reserved word

/*Omitted below*/

How to process annotations

private Object mapEnum(Class<?> clz, String columnValue) {

    String finalColumnValue = columnValue;
    columnValue = Arrays.stream(clz.getFields()) //Get all fields
            .filter(it -> {
                //When MapByValue annotation is attached and its value is equal to colmunValue
                MapByValue mapByValue = it.getAnnotation(MapByValue.class);
                return mapByValue != null && mapByValue.value().equals(finalColumnValue);
            .map(Field::getName) //Use the name of the given field

    return clz.cast(Enum.valueOf((Class<Enum>) clz, columnValue));

protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException {
    Class<?> clz = pd.getPropertyType(); //Get type information


    if (clz.isEnum()) { //For enum
        byte[] bytes = rs.getBytes(index);
        if (isEmpty(bytes)) {
            return null;

        return mapEnum(clz, new String(bytes, Charset.forName("UTF-8")));


Another idea

Since all ʻenum had a function to convert from value to ʻenum for the convenience of converting from Json, I thought about using this from reflection, but it is more versatile to process with annotations. I decided. I wish I could do valueOf ʻOverride` ...

Articles that I used as a reference

-Annotation of Enum constant -Yamkazu's Blog

Recommended Posts

[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition
What if the results of sum and inject (: +) are different?
What to do if the changes are not reflected in the jar manifest file
What to do if you can't get the text of an element in Selenium
What to do if you cannot execute with the command "Java package name / class name"
[Maven] What to do if you are asked to incorporate a jar that is not in the remote repository into the war
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
[Rails] What to do if data is not registered in DB
What to do when the changes in the Servlet are not reflected
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 2]
Determine if the strings to be compared are the same in Java
What to do if you forget the root password in CentOS7
What to do if the prefix c is not bound in JSP
The problem that the contents of params are completely displayed in the [Rails] view
The story of forgetting to close a file in Java and failing
This and that of the implementation of date judgment within the period in Java
Get the value of enum saved in DB by Rails with attribute_before_type_cast
What are the updated features of java 13
What to do when rails db: seed does not reflect in the database
If you are using Android Room and want to change the column definition
10 barrages of drawing with ● or ■ that are likely to appear in training (Java)
What to do if the server tomcat dies
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
What to do if the JSONHint annotation does not work with lombok and JSONIC
[Java] The problem that uploaded images are not updated due to the influence of cache
What to do if the breakpoint is shaded and does not stop during debugging
What to do if you select a JRE in Eclipse and get "The selected JRE does not support the current compliance level 11"
What to do if the debug gem installation fails
What to do if the Rails server can't start
What to do if password authentication fails in Docker/Postgres
Java: Use Stream to sort the contents of the collection
A program (Java) that outputs the sum of odd and even numbers in an array
What to do if you don't see the test code error message in the terminal console
Shout Java at the heart of technology-Themes and elemental technologies that Java engineers should pursue in 2017-
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
How to write comments in the schema definition file in GraphQL Java and reflect them in GraphQL and GraphQL Playground
What should I do after January 2019 regarding the Java payment issue and Java 8 end of support issue?
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
Memo that transitions to the login screen if you are not logged in with devise
[Rails] Why using find_by in scope and returning all records instead of nil if there are no records that match the conditions
What to do if Could not find hoge in any of the sources Run `bundle install` to install missing gems. Appears in the docker container
How to get the class name / method name running in Java
What to do if you get a java.io.IOException in GlassFish
I tried to summarize the basics of kotlin and java
Command to check the number and status of Java threads
What happened in "Java 8 to Java 11" and how to build an environment
# What to do if you accidentally do rails db: migrate: drop
How to derive the last day of the month in Java
What to do if the adb command cannot be executed
What to do if you can't use the rails command
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
[Java] I want to check that the elements in the list are null or empty [Collection Utils]
In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]
Static function to check if the RGB error of BufferdImage is within the specified ratio in Java
[Java] What to do if a lot of "File is opened too much" is displayed due to FileNotFoundException
[Reading memo] System design principles that are useful in the field- "Small and easy to understand"
Method definition location Summary of how to check When defined in the project and Rails / Gem
Read the data of Shizuoka point cloud DB with Java and try to detect the tree height.