[英]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.