Get enum by its inner field

后端 未结 5 1340
清歌不尽
清歌不尽 2020-11-27 15:48

Have enum with inner fields, kind of map.

Now I need to get enum by its inner field.

Wrote this:

package test;

/**
 * Test enum to test enum         


        
5条回答
  •  悲哀的现实
    2020-11-27 16:54

    You should have a HashMap with the numbers as keys and the enum values as values.

    This map can typically be in your repository. Then you can easily replace an int variable from the database with your preferred enum value.

    If your keys (int values) are stored in a database, then I will say its bad design to carry those keys around in an enum on your business layer. If that's the case, I will recommend not to store the int value in the enum.

提交回复
热议问题