我試圖打電話Entitymanager.merge()
用自定義對象,但我不斷收到以下錯誤:的PersistenceException並呼籲拋出:IllegalArgumentException用的EntityManager
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
吸氣不能稱爲莫名其妙地返回的值是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" />
What co導致這個問題,有沒有辦法解決它?
錯誤發生在兩次調用中的第一次:在merge中。所以,雖然我同意,但這不是理由,使用merge和persist都沒有意義。編輯我的帖子以避免誤解。 – antarkt1s