【发布时间】:2018-01-09 08:22:22
【问题描述】:
我正在尝试使用自定义对象调用 Entitymanager.merge(),但我不断收到以下错误:
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of my.package.model.offer.Cost.id
java.lang.IllegalArgumentException: Can not set int field my.package.model.offer.Cost.id to java.lang.String
好像Cost.id的getter不能被调用,莫名其妙的返回值是String,但应该是int。
这是我的模型类Cost:
@Entity
public class Cost implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Lob
@Column(length = 65535)
private String description;
public Cost(String description) {
this.description = description;
}
public Cost() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
这是我的控制器(ManagedBean),它试图调用EntityManager.persist():
@ManagedBean(name = "offerController")
@SessionScoped
public class OfferController {
@PersistenceContext
private EntityManager em;
private Offer offerCurrent = new Offer();
public String saveCosts() {
offerCurrent.setCosts(costsCurrent);
try {
utx.begin();
offerCurrent = em.merge(offerCurrent);
utx.commit();
} catch (NotSupportedException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (RollbackException e) {
e.printStackTrace();
} catch (HeuristicMixedException e) {
e.printStackTrace();
} catch (HeuristicRollbackException e) {
e.printStackTrace();
}
return Links.OFFERS.getLink();
}
}
这是我的JSF页面上的按钮,调用了saveCosts()方法:
<p:commandButton action="#{offerController.saveCosts()}" value="Save" />
什么可能导致这个问题,有没有办法解决它?
【问题讨论】:
标签: java hibernate jpa entitymanager