我想在数据库中保存我的menuItem对象。其中一个itemFields是一个ItemType字符串,它由ItemType枚举定界。这是我的MenuItem POJO。不能将枚举转换为字符串,同时在休眠中持久化对象
public class MenuItemImpl implements MenuItem{
private long id;
private String itemName;
private String itemType;
private int itemPrice;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName= itemName;
}
public ItemType getItemType() {
return ItemType.valueOf(itemType);
}
public void setItemType(ItemType itemType) {
this.itemType=itemType.toString();
}
public int getItemPrice() {
return this.itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice=itemPrice;
}
}
这是我的ItemType枚举
public enum ItemType {
BURGER("BURGER"), BEVERAGE("BEVERAGE"), SNACK("SNACK"), TOY("TOY");
private String value;
private ItemType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return value;
}
}
现在,当我尝试做此对象的保存。我得到以下例外:
com.saggezza.ItemType cannot be cast to java.lang.String
我不明白为什么它甚至应该尝试投射ItemType。因为我在menuItem POJO中的getter和setter方法已经将enum转换为字符串和字符串转换为enum。
任何想法?
可能重复[Hibernate枚举映射](http://stackoverflow.com/questions/1593929/hibernate-enum-mapping) –