gpt4 book ai didi

java - eclipselink 和 jpa : select id association with left join return "0" instead null

转载 作者:行者123 更新时间:2023-11-29 08:58:35 25 4
gpt4 key购买 nike

我有一个 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

我可以设置一些设置以远离解决方法吗?

最佳答案

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

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

对实体的更改应该很容易——定义 Integer id; (如果可以在 id 中使用大值,则为 Long)并更新访问器。

关于java - eclipselink 和 jpa : select id association with left join return "0" instead null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18881004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com