0
我有一個問題,與實體具有映射到數據庫的INT類型的字段不更新OpenJPA的實體。問題是,當我將該字段更新爲值(例如1337)時,它正確地保留在數據庫中,但當我將其設置爲零時,情況並非如此。當值爲零
更具體:
- 我有我的數據庫與數據類型爲int類型的字段「記錄」,並用NULL值的行
- 我檢索行作爲一個管理實體「anEntity 「
- 當我做‘anEntity.setRecord(1337)’,記錄值在DB改變,但是當我這樣做是不是這樣‘anEntity.setRecord(0)’(它在DB保持NULL)。
有沒有辦法解決呢?
一些代碼,誰更新:
@Stateless
public class AClass {
@EJB
ARepository arepository;
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void produceOutgoingMessage(int idOfOutputToProduce) {
AnEntity anEntity = arepository.find(idOfOutputToProduce);
//...
doSomething(anEntity);
}
private void doSomething(AnEntity anEntity) {
//...
anEntity.setRecord(0); // record stays NULL in DB
anEntity.setRecord(1337); // record is correctly set in DB
}
}
與實體:
@Entity
@Table(name = "MY_TABLE")
public class AnEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "C_I_IDF")
private Long id;
@Column(name = "N_I_RECORD")
private int record;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public int getRecord() {
return record;
}
public void setRecord(int record) {
this.record = record;
}
}
我已經添加了一個代碼示例,因爲我不允許發佈原始代碼:( – ODT
我添加了實體 – ODT
首先,「int」不應該在數據庫中允許「NULL」,所以模式是與類不一致。其次,如果字段設置爲0表示不更新觸發那麼就是在你的JPA提供一個錯誤......但你可能會發現,固定在第一部分(非空)主罰第二個照顧。 –