我想用Hibernate從表中檢索大約1億行。我有一個持久實體項目,其中包含一個費用集合(另一個持久實體)。鑑於我將遍歷結果和訪問費用爲每個對象,我想急切地收取費用,以避免n + 1問題。用ScrollableResults抓取Hibernate中的集合
我還應該提及,我想將它加入另一個名爲Provider(一對一映射但不包含外鍵)的表中。我嘗試過:
String query = "select new " + Order.class.getName()
+ "(i, p) from Item i left join fetch i.fees f, Provider p where "
+ "p.factoryId=i.factoryId and p.factoryRef=i.factoryRef";
return session.createQuery(query).scroll();
我的訂單類包含提供者字段和項目字段。我得到這個錯誤:
Caused by: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
我想與訂單包含項目(使用費預先抓取)和供應商的可滾動列表,結束了。
我認爲這個答案是正確的。如果是這樣,你應該可以讓Hibernate知道你實際上通過改變查詢返回一個List而不是構造一個Order來獲取'Item':'select i,p from ...'。您將不得不手動創建'訂單'。 – 2012-08-11 10:30:21