2015-05-28 33 views
0

試圖子查詢的時候,我有兩個實體投標,並BidStatus,投標有屬性的長期與BidStatusUnvalid語法異常在JPQL

Set<BidStatus> Bid.bidStatuses [OneToMany] 
Bid BidStatus.parentBid [ManyToOne] 

一個一對多的關係BidStatus有:

  • 標籤:字符串
  • 日期:日期(LOCALDATE的)

什麼我'嘗試做我■選擇所有狀態爲「標籤=狀態」且日期介於日期前和日期後之間的所有出價。

現在我嘗試以下查詢:

select b from Bid b 
    where b.statuses in (select S from BidStatus S 
           WHERE 
              S.label = :status 
             and S.date >= :before 
             and S.date <= :after) 

但使用此查詢我有以下異常:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement 

編輯: 我能使其運行使用以下查詢:

select b from Bid b 
      where EXISTS " + 
       (select S from BidStatus S 
        WHERE 
           S.label = :status 
          and S.date >= :before 
          and S.date <= :after 
          and S.parentBid = b) 

但我仍然不明白第一個錯在哪兒。

+1

「IN」用於檢查集合中是否存在元素;你似乎想檢查一個集合中的集合的存在 - 因此這是錯誤的假設。我還假設「b.statuses」是「b.bidStatuses」,以匹配您所說的字段。 –

+0

你是對的我沒有正確使用IN運算符:) – mteffaha

回答

0

從A 選擇其中 a.bs(從B B的選擇B WHERE
BX =:參數1 和> =:paramDate1 和由< =:paramDate2)

你需要使用別名'a'來引用'bs'而不是實體類名'A',即。 a.bs而不是A.bs

+0

謝謝,但這不是我的問題,我試圖簡化查詢,但在這樣做,我明確地讓你更難以幫助,所以現在我把實際的查詢 – mteffaha