I am trying to persist my menuItem Object in the database. One of the itemFields is an ItemType string which is bounded by ItemType enum. Here is my 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;
}
}
This is my ItemType enum
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;
}
}
Now when I try to do a save on this object. I get the following exception:
com.saggezza.ItemType cannot be cast to java.lang.String
I dont see why it should even try casting ItemType. as my getter and setter methods in the menuItem POJO already take care of the conversion of enum to string and string to enum.
Any ideas?