【问题标题】:eclipselink and jpa: select id association with left join return "0" instead nulleclipselink 和 jpa:选择 id 关联与左连接返回“0”而不是 null
【发布时间】:2013-09-18 19:44:53
【问题描述】:

我有一个 JPA 选择:

select c.name, f.id from Child c left join c.father

预期结果是:

Child 1  |  1
Child 2  |  2
Orphan   | null

但我抓住了

Child 1  |  1
Child 2  |  2
Orphan   |  0  // ZERO ?

我能够解决这个选择

select c.name, case when (f.id is null) then null else f.id end from Child c left join c.father

我可以设置一些设置以避开解决方法?

【问题讨论】:

  • f.id的java类型是什么?
  • 类型是“int”,但是,使用“case”我可以验证真实值(null)
  • 尝试使用 Integer 而不是允许空值的 int。

标签: java jpa jpa-2.0 eclipselink


【解决方案1】:

正如 cmets 中提到的,问题在于原语的使用。基元只能存储默认设置为“某个值”的值。在 int 的情况下,这是一个 0(long 为 0,boolean 为 false 等等)。

为了将数据库列表示为空,相关实体应使用 Integer 对象。这允许设置 null,也可以分配值。

对实体的更改应该很容易-将定义定义为整数ID; (或 Long 如果可以在 id 中使用大值)并更新访问器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多