2013-10-03 150 views
2

我需要一些幫助來設置我的查詢。如果我可以避免它,我不想讓多個選擇基本上形成相同的子查詢。在堅果殼中,我有用於跟蹤幾個細節的名爲TimeSlot的對象。那些TimeSlot的是支付的項目。當TimeSlot提交報銷時,他們用於創建PayableTimeSlot。在支付TimeSlot之前,我需要確保它尚未支付。JPA避免多個子查詢

因爲它位於下面是我的查詢:

@NamedQuery(
    name = "TimeSlot.by.person.academy.id.by.contract.date", 
    query = "select distinct ts 
     from TimeSlot ts 
     join ts.invitedInstructors ii 
     join ts.academyClass ac 
     join ac.academy a 
     where ii.person.id = ? 
     and a.id = ? 
     and ts.schedule.startDateTime BETWEEN ? AND ? 
     and ts.id not in (select e.id from PayableTimeslot pts join pts.event e) 
     and ? not in (select e.claimant from PayableTimeslot pts join pts.event e)") 

正如你可以看到我已經選擇從PayableTimeSot元素的第一not in。有沒有辦法將子查詢擴展爲: (select e.id, e.claimant from PayableTimeslot pts join pts.event e)我只是不確定如何檢查多個項目not in的子查詢。無論如何,如果對問題的解決方法比我所做的更好,請告訴我。

除非你們都認爲多重選擇不會成爲大問題......每個表格每週平均會有30-50個條目被複制(審覈跟蹤)的時間會超過7-9次。

回答

0

好吧,經過一番思考後,我想出了這個。我確實試圖以錯誤的方式回答這個問題......當我所需要的只是一個在第一個位置上的位置時,我正在做兩個子查詢,從而將兩者結合起來。

@NamedQuery(
    name = "TimeSlot.by.person.academy.id.by.contract.date", 
    query = "select distinct ts " 
    + "from TimeSlot ts " 
    + "join ts.invitedInstructors ii " 
    + "join ts.academyClass ac " 
    + "join ac.academy a " 
    + "where ii.person.id = ? " 
    + "and a.id = ? " 
    + "and ts.schedule.startDateTime BETWEEN ? AND ? " 
    + "and ts.id not in (select e.id from PayableTimeslot pts join pts.event e where pts.claimant = ?)")