2013-10-29 17 views
0

我開發一個JSF項目,並使用Hibernate對MySQLHQL NamedQuery與內上一個HashMap

正如你可能知道(它是在Hibernate文檔中)加入,加入的實體之間使用的關聯。因此,與內部的樣品正確的查詢加盟將是:

select from Person p join p.classes c where c.room = :roomNum 

BU在我的情況下,相關的實體是包含所需的實體是一個HashMap。 一些代碼將有助於:

public FamilyGuy{ 

private String name; 
private BigDecimal income; 
private HashMap<String, Child> children = new HashMap<Language, Child>(); 
.... 
} 

public Child{ 
private String name; 
private BigDecimal expenses; 
.... 
} 

我需要的是這樣的查詢(下面的查詢是不工作):

select from FamilyGuy oppressed inner join Child happy where happy.expenses < :threshold 

我得到的例外是:

javax.servlet.ServletException: Path expected for join! 

任何幫助將不勝感激。

回答

1
select f from FamilyGuy f 
inner join f.children child 
where child.expenses < :threshold 

就像與任何其他toMany協會。

正如你所說;加入使用關聯。因此,您不能指定實體的名稱(join Child),但必須指定關聯:join f.children ...就像在問題開始時的示例一樣。

+0

感謝您提供簡短而成熟的答案,在您的查詢中,您是否選擇了FamilyGuy?如果我想選擇孩子將這個查詢是正確的:從FamilyGuy中選擇孩子f 內部加入f.children孩子 where child.expenses <:閾值 – MoienGK

+0

是的,這是正確的。 –