Search code examples
jpaentityopenjpa

OpenJPA Entity not updated when value is zero


I've an issue with an Entity which has a field mapped to an INT type in DB. The problem is that when I update the field to a value, like 1337 for instance, it's correctly persisted in the DB but it's not the case when I set it to zero.

To be more specific:

  • I have a row in my DB with a field "record" that is INT type and with a NULL value
  • I retrieve that row as a managed Entity "anEntity"
  • When I do "anEntity.setRecord(1337)", the record value is changed in DB but it's not the case when I do "anEntity.setRecord(0)" (it stays NULL in DB).

Is there a way to fix it ?

a bit of code, the one who updates:

@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
    }
}

And the Entity:

@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;
    }
}

Solution

  • An "int" field should never be configured as allowing NULL in the database. This is likely the cause of the problems. Fix that and it should work.

    If it doesnt then report a bug in OpenJPA.