JPA 连接具有空值的列
我有一个查询可以在普通 SQL 中运行,但不能在 JPA 中运行,并且无法弄清楚原因。正如您可以从标题中猜到的那样,我有一个线索,但我不知道如何“修复”它。
这是实际的重要代码:
@Id
@Basic(optional = false)
@Column(name = "id", nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "read_permission", nullable = false)
private boolean readPermission;
@Basic(optional = false)
@Column(name = "write_permission", nullable = false)
private boolean writePermission;
@Basic(optional = false)
@Column(name = "execute_permission", nullable = false)
private boolean executePermission;
@Basic(optional = false)
@Column(name = "admin_permission", nullable = false)
private boolean adminPermission;
@JoinColumn(name = "xinco_core_data_id", referencedColumnName = "id", nullable=true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreData xincoCoreDataId;
@JoinColumn(name = "xinco_core_group_id", referencedColumnName = "id", nullable=true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreGroup xincoCoreGroupId;
@JoinColumn(name = "xinco_core_node_id", referencedColumnName = "id", nullable=true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreNode xincoCoreNodeId;
@JoinColumn(name = "xinco_core_user_id", referencedColumnName = "id", nullable=true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreUser xincoCoreUserId;
这是工作sql:
select * from xinco_core_ace where xinco_core_user_id = 1 order by xinco_core_user_id, xinco_core_node_id, xinco_core_data_id;
这是我正在尝试做的事情:
SELECT xca FROM XincoCoreAce xca WHERE xca.xincoCoreUserId.id = 1 ORDER BY xca.xincoCoreUserId.id, xca.xincoCoreGroupId.id, xca.xincoCoreNodeId.id, xca.xincoCoreDataId.id
我认为问题在于 xca.xincoCoreUserId.id、xca.xincoCoreGroupId.id、xca.xincoCoreNodeId.id、xca .xincoCoreDataId.id 可以为空。
有什么想法吗?希望更容易阅读:P
I have a query that works in plain SQL but is not working on JPA and can't figure out why. As you can guess from the title I have a clue but I don't know how to "fix" it.
Here's the actual important code:
@Id
@Basic(optional = false)
@Column(name = "id", nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "read_permission", nullable = false)
private boolean readPermission;
@Basic(optional = false)
@Column(name = "write_permission", nullable = false)
private boolean writePermission;
@Basic(optional = false)
@Column(name = "execute_permission", nullable = false)
private boolean executePermission;
@Basic(optional = false)
@Column(name = "admin_permission", nullable = false)
private boolean adminPermission;
@JoinColumn(name = "xinco_core_data_id", referencedColumnName = "id", nullable=true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreData xincoCoreDataId;
@JoinColumn(name = "xinco_core_group_id", referencedColumnName = "id", nullable=true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreGroup xincoCoreGroupId;
@JoinColumn(name = "xinco_core_node_id", referencedColumnName = "id", nullable=true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreNode xincoCoreNodeId;
@JoinColumn(name = "xinco_core_user_id", referencedColumnName = "id", nullable=true)
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreUser xincoCoreUserId;
And here's the working sql:
select * from xinco_core_ace where xinco_core_user_id = 1 order by xinco_core_user_id, xinco_core_node_id, xinco_core_data_id;
And here's what I'm attempting to do:
SELECT xca FROM XincoCoreAce xca WHERE xca.xincoCoreUserId.id = 1 ORDER BY xca.xincoCoreUserId.id, xca.xincoCoreGroupId.id, xca.xincoCoreNodeId.id, xca.xincoCoreDataId.id
The issue, I think, is that the xca.xincoCoreUserId.id, xca.xincoCoreGroupId.id, xca.xincoCoreNodeId.id, xca.xincoCoreDataId.id can be nulls.
Any idea? Hopefully is easier to read :P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到了一个更简单的查询:
发现任何属性worker为null(任务未分配)的任务结果实体都将被丢弃。后来我发现这是因为 JPA 中的路径导航是使用内部联接完成的(规范是这么说的),这将排除路径部分为空的任何结果。
这个错误报告准确地描述了这个问题:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=363798
不幸的是,这不是一个实现错误,您必须重构您的实体/查询以避免此类情况。
Also happened to me with a simpler query:
Found that any task result entity where the attribute worker is null (the task is unassigned) would be discarded. Later I found out that this is because path navigation in JPA is done using inner joins (the specification says so) and this will exclude any result where a part of the path is null..
This bug report describes accurately the issue:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=363798
Unfortunately, this is not an implementation bug and you will have to refactor your entities/queries to avoid these kind of situations.
这是实际完成的查询(使用 eclipselink 日志记录):
出于某种原因,使用 Order by 会添加大量表交叉检查,其中包含 null 会使结果为空。
删除订单即可得到所需的结果(当然是乱序的)。
请参阅此 Eclipselink bug
This is the actual query done (using eclipselink logging):
For some reason having the Order by adds a lot of table crosschecking in which having nulls make the result come out empty.
Removing the order by gets the required result (out of order of course).
See this Eclipselink bug