JAVA_CAT
JAVA_CAT

Reputation: 859

Null value to an Int field

I have an entity class which has an int field , in the table the cloumn type is Number (10,0). In the table default value is "NULL". Using Spring data JPA when I try select query using jpa I am getting below errors.

java.lang.IllegalArgumentException: Can not set int field com.test.app.entity.TestProject.baseUserIdNumber to null value

I cannot change anything in the table as this is already created and used in the production. Anything I can do with Entity class or JPQL

@Entity
public class TestProject {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int projectId;
    private String description;
    private String name;
    private int seqNumber
    private LocalDate startDateTime;
    private LocalDate endDateTime;
    @Column(name = "baseUserIdNumber")
    private int myfield;
    private String projStatus;



constructors ()
getters()
Setters()


}

Upvotes: 3

Views: 7244

Answers (3)

prashant.kr.mod
prashant.kr.mod

Reputation: 1722

use wrapper, Integer instead of int

Upvotes: 0

Jason
Jason

Reputation: 5244

You can just define the field with a default value and define as int instead of Integer so that it doesn't accept NULL values.

@Column(name = "my_column")
private int myField = 0;

Upvotes: 4

Littlefoot
Littlefoot

Reputation: 143103

There's something strange in what you described.

  • there's a table
  • it has a column whose datatype is number(10, 0)
  • its default value is null
    • well, yes - if it isn't specified, it really is null
  • code you executed returns

    Can not set (...) to null value

That just doesn't make sense. If its default value is null, how come you can't set it to null? Isn't the table set to not null, perhaps?

This is Oracle code, but the error message is similar to what you got:

SQL> create table test (id number(10, 0) not null, name varchar2(20));

Table created.

SQL> insert into test
  2    select 1, 'Little' from dual union all
  3    select 2, 'Foot'   from dual;

2 rows created.

SQL> update test set id = null where id = 1;
update test set id = null where id = 1
                *
ERROR at line 1:
ORA-01407: cannot update ("SCOTT"."TEST"."ID") to NULL

As you can't do anything with the table (as it is used in production), all you can do is not trying to set it to null (no matter which tool or language you use).

Upvotes: 0

Related Questions