9

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

2 Answers2

13

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
4

You can index into Enum.values():

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

You might want to put in some error checking. :)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521