【问题标题】:Can not set int field to null value不能将 int 字段设置为空值
【发布时间】:2023-03-06 17:55:01
【问题描述】:

我有这个int 专栏:

@Column(length = 4)
private int contract_owner_id;

我不需要为每个表格行设置总是值。进行选择查询时出现此异常:

Can not set int field org.entity.contracts.contract_owner_id to null value

当表格列中没有数据时,有什么方法可以设置 numb 设置?

【问题讨论】:

  • 请把int改成IntegerLong好吗
  • 在 JPA 中使用 int 和 Integer 有什么区别?
  • int 是原始类型而不是对象
  • 虽然 int 和 Integer 用于存储整数类型数据,但两者之间的主要区别在于 int 类型是原始类型,而 Integer 是类类型。所以 int 不能为空。

标签: java hibernate spring-data-jpa


【解决方案1】:

使用Integer 代替int

前-:

私有整数contract_owner_id;

【讨论】:

    【解决方案2】:

    原始数据类型 int 不是 可为空的。在这种情况下,您需要使用 Wrapper 类“Integer”。所以只需替换类型即可。

    【讨论】:

    • 是否有一些配置可以让 int 为空?
    • 你是什么意思?
    • @PeterPenzov 请尝试解决方案
    • @PeterPenzov int 不是对象。所以不能为空。
    • 避免 Hibernate 尝试将 null 值设置为原语的最佳方法是使用 Wrapper 类。自动装箱是您的朋友。
    猜你喜欢
    • 2014-08-07
    • 2015-07-10
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多