Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有空值的JPA联接列_Java_Orm_Join_Null - Fatal编程技术网

Java 具有空值的JPA联接列

Java 具有空值的JPA联接列,java,orm,join,null,Java,Orm,Join,Null,我有一个查询可以在纯SQL中工作,但不能在JPA上工作,我不知道为什么。正如你可以从标题中猜到的,我有一个线索,但我不知道如何“修复”它 下面是实际的重要代码: @Id @Basic(optional = false) @Column(name = "id", nullable = false) private Integer id; @Basic(optional = false) @Column(name = "read_permission", nullable = false

我有一个查询可以在纯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

我在这里不是JPA专家,但我会尝试获取在数据库上运行的实际SQL。可能FetchType.LAZY与问题有关?

这是实际完成的查询(使用eclipselink日志记录):

出于某种原因,使用orderby会增加很多表交叉检查,其中使用null会使结果为空

删除orderby将获得所需的结果(当然是无序的)


请看这个日食也发生在我身上,一个更简单的查询:

select t from Task t where t.worker is not null order by t.worker.normalizedName
发现属性worker为null(任务未分配)的任何任务结果实体都将被丢弃。后来我发现这是因为JPA中的路径导航是使用内部联接完成的(规范中这样说),这将排除路径部分为null的任何结果

此错误报告准确描述了问题:


不幸的是,这不是一个实现错误,您必须重构实体/查询以避免此类情况。

一般来说,在问题中进行代码转储是一个坏主意。您应该将代码缩减到最低限度,以演示问题。如果没有其他东西,它会让你更容易阅读和理解(因此它更容易被阅读和回答),如果没有其他东西,它会证明你已经付出了一些努力。干杯。感谢您的反馈,这只是我的第二个问题,也是第一个相关代码的问题。希望现在更干净了。如果您使用Hibernate作为JPA提供商,您可以看看它有点旧了,所以我希望它仍然有效。我正在使用EclipseLink,但无论如何我都要看一看。这是实际完成的查询(使用EclipseLink日志记录):选择t1.id、t1.write\u权限、t1.admin\u权限、t1.execute\u权限、t1.read\u权限,t1.xinco_核心_用户id、t1.xinco_核心_数据id、t1.xinco_核心_组id、t1.xinco_核心_节点id来自xinco_核心_数据t4、xinco_核心_节点t3、xinco_核心_组t2、xinco_核心_ace t1、xinco_核心_用户t0,其中((t3.id=?)和((t3.id=t1.xinco_核心_节点id=t1.xinco_节点id)和(t0.co_用户t1=t1(t4.id=t1.xinco_core_data_id)))按t0.id ASC、t2.id ASC、t3.id ASC、t4.id ASC bind=>排序[1]我对数据库运行了该查询,并返回了一个空结果。我将继续查看SQL以查看错误。很明显,如果某些值为null,它们将使某些where语句失败。如果我删除所有where语句excpet t3.id=?它会起作用。我将尝试删除惰性部分,看看会发生什么。
select t from Task t where t.worker is not null order by t.worker.normalizedName