2013-09-22 37 views
0

我有一個關於HQL的問題。假設,我有兩個表。例如,表「人員」和表格「手機號碼」。一人多手機號碼。關係是@OneToManyHQL選擇何時映射對象通過使用註釋

在我Person.java類,它是實體我有一些領域和領域之一是

List<MobileNumber> mobiles; // (list of mobile numbers). 

問題是這樣的: 我想在HQL的MobileNumber.java類的領域之一來訪問。像這樣:

Query = EntityManager.createQuery("select p.someField from Person where p.mobiles.someField = "value"); 

MobileNumber對象映射到Person對象。正如我所提到的,關係是OneToMany和Person包含MobileNumbers列表。如果關係是OneToOne或ManyToOne,這不是問題,因爲在這種情況下,Person對象只包含MobileNumber的一個對象。

在此先感謝。

回答

1

你可以在其他實體

SELECT mobile.someField 
FROM Person person 
JOIN person.mobiles mobile 
WHERE mobile.someField = "value" 

Read more about it in the documentationJOIN

+0

謝謝,但我沒有使用JOIN語句。而不是我使用註釋。 – Chala

+0

@Chala你正在使用HQL。您需要使用適當的HQL語法來實現您的目標。這個用例與註釋無關。 –

+0

如果我在這個陳述中使用註釋,可以嗎? – Chala