How to pass results to rowmapper in enum?

I have a cool line like:

private static final class UserRowMapper implements RowMapper<User> { User user = new User(); user.setId(rs.getInt("id")); user.setUserType( (UserType)rs.getInt("userType")); // ????? return user; } 

So, I am trying to apply an integer value in db for userType to a UserType enumeration.

Why is this not working?

+4
source share
2 answers

Drop it? No, it cannot be done.

You can call valueOf to get the Enum value from String if the string is valid.

+7
source

You can index in Enum.values() :

 user.setUserType( UserType.values()[rs.getInt("userType")] ); 

You might want to check out some errors. :)

+2
source

Source: https://habr.com/ru/post/1392398/


All Articles