2017-07-03 115 views
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; 
    } 
} 
+0

我已经添加了一个代码示例,因为我不允许发布原始代码:( – ODT

+0

我添加了实体 – ODT

+0

首先,“int”不应该在数据库中允许“NULL”,所以模式是与类不一致。其次,如果字段设置为0表示不更新触发那么就是在你的JPA提供一个错误......但你可能会发现,固定在第一部分(非空)主罚第二个照顾。 –

回答

0

的 “int” 字段不应该被配置为允许NULL在数据库中。这可能是问题的原因。修复它,它应该工作。

如果它不那么OpenJPA报告错误。

相关问题