2012-11-15 37 views
1

希望有些Hibernate專家能夠提供幫助。通過HQL的Hibernate ScrollableResults需要每個關聯都需要左連接提取

我有一個對象結構,看起來像這樣:

class Customer { 
    private Stuff1 stuff1; 
    private Stuff2 stuff2; 

    // Bazillion other fields 
} 

class Report { 
    private Customer customer; 
    private String uniqueBatchId; 

    // Bazillion other fields 
} 

class AbstractSpecialReport { 
    private Report report; 

    //Bunch of other fields 
} 

class SpecialReport extends AbstractSpecialReport{ 
    private List<ReportParts> reportParts;   
} 

所有三個標註爲Hibernate的實體和所有關聯的默認值(使用Hibernate 3.2,所以應該是默認的懶

我要使用這個HQL得到的結果,使用。我已經做了必要的咒語來獲得結果集流。

select srpt 
from SpecialReport as srpt 
left join fetch srpt.report as rpt 
left join fetch rpt.customer as cst 
left join fetch cst.stuff1 
left join fetch cst.stuff2 
where rpt.uniqueBatchId = :batchId 

但是我得到「java.sql.SQLException:流結果集[email protected]仍然處於活動狀態。當任何流式結果集打開並在給定連接上使用時,不會發布任何語句。確保您在嘗試更多查詢之前在任何活動流式結果集上調用了.close()。「

我有SQL查詢日誌記錄,我可以清楚地看到它試圖在之後取回報告上不相關的屬性滾動前進。

於是我就開始我的成長HQL包括這些領域。

現在看起來

select srpt 
from SpecialReport as srpt 
left join fetch srpt.report as rpt 
left join fetch rpt.customer as cst 
left join fetch rpt.field1 
left join fetch rpt.field2 
left join fetch rpt.field3 
left join fetch rpt.field4 as child 
-- now field 4 was an object that also has fields that are associations. 
left join fetch child.field1 
left join fetch child.field2 
-- ad nauseum, this keeps going down the object tree 
left join fetch cst.stuff1 
left join fetch cst.stuff2 
where rpt.uniqueBatchId = :batchId 

即使有大約25加入我還有更多的領域是被加載導致相同的異常。 我可以繼續手動走圖,但這是永遠的,我覺得這不應該是必要的。通過Hibernate代碼看起來,滾動調用的TwoPhaseLoad嘗試初始化對象中的所有代理和延遲加載的字段,這必然會打破在滾動運行時不執行其他SQL查詢的要求。這對那裏的任何人有意義嗎?

回答

1

XxxToOne關聯默認情況下是渴望的。如果你不希望Hibernate熱切地加載它們,將它們標註爲懶惰。

此外,SpecialReport是一個報告,所以下面聯接並沒有太大的意義:

from SpecialReport as srpt 
left join fetch srpt.report as rpt 
+0

那是我的代碼的草圖錯誤,SpecialReport不會在現實中延伸報告。我會檢查* ToOne協會,但沒有意識到他們是Eager。 – 3martini