2013-02-18 68 views
0

Let'say我有以下表格:JPQL查詢

Table A: id b-id 
Table B: id property 

我可以過濾像JPQL語言如下表A的元素?

選擇一個從一個JOIN AB-ID targetID其中targetId.property =:someValue中

我想獲得其中所引用的B元素具有屬性= someValue中

如果表A的元素我介紹第三個表

Table A: id b-id 
Table B: id c-id 
Table C: id property 

我怎樣才能獲得A的元素,其中c.property = someValue中?

我開始感受到ORM的力量,但有些概念對我來說依然模糊。 謝謝您的回答

回答

1

JPQL查詢對實體而不是數據庫表操作。我假設實體和持久屬性的名稱與給定的表和數據庫列的名稱匹配。

因爲在問題的所有關係是單值的,一到一個或多個對一個(每個A僅連接到一個B(或可能不存在),每B被連接到一個C),在查詢中指定連接根本不需要。

選擇一個從一個WHERE abcproperty = someValue中

沒有必要擔心在路徑空值,因爲在JPA 2.0規範說:

Path expression navigability is composed using 「inner join」 semantics. 
That is, if the value of a non-terminal field in the path expression is null, 
the path is considered to have no value, and does not participate in the 
determination of the result. 

同爲不起作用集合值屬性(一對多,多對多),因爲無法通過路徑表達式導航到它們的屬性。

+0

這是一個非常準確的答案,謝謝!要將主題擴展到集合,您能否說明集合屬性會發生什麼? – facewindu 2013-02-20 07:04:01