I have a table roles
which has values such as admin
, user
, and others. I do not want to make it an ENUM
type because I think the list might grow, shrink or change in the near future. I can't figure out a way to get Hibernate to Map directly to their respective enum types in my code automatically when I get the queried object.
Is there a standard approach to this?
EDIT: So basically, instead of storing the string in the row (which is what is on the DB), I want it to store the Enum when I query the DB.
public class enum UserType {
Admin("Admin"),
User("User");
private final String userType;
UserType(String userType) {
this.userType = userType;
}
@Override
public String toString() {
return userType;
}
}
@Entity
@Table(name = "user")
public class User {
private Int id;
private UserType userType;
public Int getId() {
return id;
}
public void setId(Int id) {
this.id = id;
}
public UserType getUserType() {
return userType;
}
public void setUserType(UserType userType) {
this.userType = userType;
}
}
since theres no code provided so im gonna assume that you want to query a data by admin or user ?
you can create an enum of role then include it to your entity class.
e.g.
Enum 1:
Enum 2:
this how i mapped enum type.
If you have knowledge about Criteria in Hibernate
here is the example to query the Card by an enum: