简体   繁体   中英

Mapping Table Rows to Enum Values in Hibernate 4

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.

eg

Enum 1:

public enum CardSuit implements Serializable{

        DIAMOND("Diamond"),HEART("Heart"),SPADE("Spade"),CLUB("Club");

        private String cardSuit;

        private CardSuit(String cardSuit){
            this.cardSuit = cardSuit;
        }

        public String toString(){
            return cardSuit;
        }

    }   

Enum 2:

public enum CardValue implements Serializable{

    ACE(1),TWO(2),THREE(3),FOUR(4),
    FIVE(5),SIX(6),SEVEN(7),EIGHT(8),
    NINE(9),TEN(10),JACK(10),QUEEN(10),KING(10);

    private int cardValue;

    private CardValue (int cardValue){
        this.cardValue = cardValue;
    }

    public int getValue(){
        return cardValue;
    }

    public String toString(){
        return String.valueOf(cardValue);
    }

}

this how i mapped enum type.

@Entity
@Table(name="CARD")
public class Card implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="CARD_ID")
    private int cardId;

    @Enumerated(EnumType.STRING)
    @Column(name="CARD_VALUE")
    private CardValue cardValue;

    @Enumerated(EnumType.STRING)
    @Column(name="CARD_SUIT")
    private CardSuit cardSuit;

    //Setter & Getter ...

}

If you have knowledge about Criteria in Hibernate

here is the example to query the Card by an enum:

sessionFactory.getCurrentSession()
            .createCriteria(Card.class)
            .add(Restrictions.eq("cardValue", CardValue.ACE))
            .list();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM