繁体   English   中英

Java:JPQL选择语句

[英]Java: JPQL select statement

select x from X x where xaid = :a_id >始终选择0个对象

为什么上面的JPQL语句不起作用,而下面的那一个却起作用?

select a from A a where a.id = :a_id a_id-> a_obj
select x from X x where xa = :a_obj >总是正确选择对象的数量

在执行过程中,两个查询都不会引发异常,但是会获得不同数量的结果。

谢谢


更新资料

我通过使用联接尝试了以下查询:
select x from X x, xa a where xaid = :a_id >用于意外令牌的TopLink异常

然后select x from X x JOIN xa a where a.id = :a_id >始终正确选择对象的数量

通过后一个查询,我已经解决了手头的最初问题。 但是,现在我有两个查询应该可以工作,但是由于某种原因没有。

select x from X x where xaid = :a_id >始终选择0个对象
select x from X x, xa a where xaid = :a_id >用于意外令牌的TopLink异常

还有其他人遇到过类似的行为吗?

对于X具有以下实体

@Entity
public class EntityX {

    @Id @GeneratedValue
    private Long id;

    @OneToOne
    private EntityA a;

    // ...
}

这是A的:

@Entity
public class EntityA {
    @Id @GeneratedValue
    private Long id;

   //...
}

以下JPQL查询:

from EntityX x where x.a.id = :id

生成以下SQL:

select
  entityx0_.id as id282_,
  entityx0_.a_id as a2_282_ 
 from
  EntityX entityx0_ 
 where
  entityx0_.a_id=?

它简单地工作并返回预期的结果。

经过Hibernate(和EclipseLink)测试。 如果这不能代表您的情况,请添加更多详细信息。

我认为您还必须在第一个示例中引入实体a,以便其属性可见。

就像是

select x from X x join fetch x.a where x.a.id = :a_id

(我不使用JPA,我坚持使用HQL,因此这未经测试,未经验证且没有退款保证。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM