How to cast resultset in rowmapper to an enum?
11,373
Solution 1
Cast it? No, can't be done.
You can call valueOf to get the Enum value from the String, as long as the String is valid.
Solution 2
You can index into Enum.values()
:
user.setUserType( UserType.values()[rs.getInt("userType")] );
You might want to put in some error checking. :)
Comments
-
Blankman almost 2 years
I have my rowmapper 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 cast the integer value in the db for userType to the enumeration UserType.
Why doesn't this work?