简体   繁体   English

Int 字段的空值

[英]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).我有一个实体类,它有一个 int 字段,在表中,cloumn 类型是 Number (10,0)。 In the table default value is "NULL".在表中默认值为“NULL”。 Using Spring data JPA when I try select query using jpa I am getting below errors.当我尝试使用 jpa 选择查询时,使用 Spring 数据 JPA 我遇到以下错误。

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我可以用实体类或 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()


}

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.您可以使用默认值定义字段并将其定义为 int 而不是 Integer 以便它不接受 NULL 值。

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

There's something strange in what you described.你描述的情况有些奇怪。

  • there's a table有一张桌子
  • it has a column whose datatype is number(10, 0)它有一个数据类型为number(10, 0)
  • its default value is null它的默认值为null
    • well, yes - if it isn't specified, it really is null嗯,是的 - 如果没有指定,它真的是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 ?如果它的默认值是null ,为什么不能将它设置为null Isn't the table set to not null , perhaps?表不是设置为not null吗?

This is Oracle code, but the error message is similar to what you got:这是 Oracle 代码,但错误消息与您得到的类似:

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).由于您无法对表执行任何操作(因为它在生产中使用),您所能做的就是不要尝试将其设置为null (无论您使用哪种工具或语言)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM