【问题标题】:Null value to an Int fieldInt 字段的 Null 值
【发布时间】:2020-03-31 13:42:10
【问题描述】:

我有一个实体类,它有一个 int 字段,表中的 cloumn 类型是 Number (10,0)。在表中默认值为“NULL”。当我尝试使用 jpa 选择查询时使用 Spring data JPA,我遇到了以下错误。

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

我无法更改表中的任何内容,因为它已在生产中创建并使用。我可以用 Entity 类或 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()


}

【问题讨论】:

    标签: java oracle entity spring-data-neo4j


    【解决方案1】:

    您可以只使用默认值定义字段并将其定义为 int 而不是 Integer,以便它不接受 NULL 值。

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

    【讨论】:

    • 为什么它不起作用?然后用代表表格的整个班级更新主帖子。
    • 当然。我会这样做的。
    • 我将 myField 数据类型更改为 BigDecimal 并且它有效。由于表格属性显示字段的数据类型为数字(10,0),我只是猜测这可能是一个小数字段。
    • 实体类已添加。当我将 int 更改为 BigDecimal 时,它工作正常。如果我能在这里使用 int 将会很有帮助。
    • 好吧,如果您期望/需要一个 NULL 值,您可以使用 Integer 而不是 int。
    【解决方案2】:

    你的描述有些奇怪。

    • 有一张桌子
    • 它有一个数据类型为number(10, 0)的列
    • 其默认值为null
      • 嗯,是的 - 如果没有指定,它确实是null
    • 你执行的代码返回

      不能将 (...) 设置为空值

    这没有任何意义。如果它的默认值是null,为什么不能设置为null?表不是设置为not null吧?

    这是 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
    

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

    【讨论】:

    • 当我尝试从表中获取数据时出现错误。该特定字段的表中的值为空。 Null 值被分配给原始 int。可能是这个问题吗?
    • 在您的示例中,您将该字段设置为非空。在我的情况下,默认值为 null。
    • 获取?哦。对我来说,它看起来像是更新/插入。以我的例子为例:是的,这就是我在阅读您描述的问题时的想法。
    • 我明白了;你说它适用于 BigDecimal。不能继续用吗?不幸的是,我不懂 Java,所以对此我无能为力。
    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 2011-07-19
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多