0

我有一个使用枚举类型的 java 对象

  public class Deal{

public enum PriceType {
        fixed, hour, month, year
    }

     @Element(name = "price-type", required = false)
     private PriceType priceType;

  }

这个对象是从一些 API 中填充的,我在具有字符串类型变量的数据库对象中检索它

MyDeal{
    private String priceType;

    public String getPriceType() {
    return priceType;
   }

    public void setPriceType(String priceType) {
    this.priceType = priceType == null ? null : priceType.trim();
   }

}

为什么我不能像这样设置我的数据库对象

 List<Deal>deals = dealResource.getAll(); 
 MyDeal myDeal = new myDeal(); 

 for (Deal deal : deals) {
     myDeal.setPriceType(deal.getPriceType());
 }
4

2 回答 2

1

将枚举添加到属性

@Enumerated(EnumType.STRING)
@Element(name = "price-type", required = false)
private PriceType priceType;
于 2013-10-23T09:26:45.330 回答
1

您不能PriceType直接将 a 设置为字符串。你需要做这样的事情

for (Deal deal : deals) {
     myDeal.setPriceType(deal.getPriceType().name()); // name() will get that name of the enum as a String
}

虽然for循环看起来有严重缺陷。你只会一遍又一遍地覆盖priceTypein 。myDeal

于 2013-10-23T09:30:22.940 回答