Hibernate: Case insensitive EnumType fields?

I'm working on a legacy DB I have no control over, in my code I have an entity "User" which is mapped to a table in the DB, User has a User.role field, this filed is an Enum:

 

public enum UserRole{ ADMIN,SUPERADMIN.....}

User has the following mapping

 

@Column(name="user_role")
@Enumerated(EnumType.STRING)
private UserRole role;

the problem is that the DB is inconsistent, in the DB i might some times get user_type as one of the following:

 

  1. Admin
  2. admin
  3. ADMIN

for the first two cases I will get an exception since hibernate would not fine matching Enum

Is there a simple way to tell hibernate to ignore cases for this enum? I know I can probably extned Hibernate's EnumType and modify it but I was wondering if there's a simple solution.

Thanks in advance,

Lior

Comments

If you don't want to use a user type, you can simply map the field as a String for Hibernate , and use the getter/setter for the ENUM for the rest of the application.

@Column(name="user_type")
private String type;

public UserType getUserType(){
   return UserType.valueOf(type.toUpperCase());
}

public void setUserType(UserType userType){
   type = userType.toString();
}

 

That would work, Thanks!