![](/img/trans.png)
[英]Many-to-Many Relationship (with properties) in Google App Engine for Java
[英]JPA and many-to-many relations in google app engine
我有實體A和B,並且A可以有B的集合。B的相同實例可以屬於幾個A。因此這里存在經典的多對多關系。
在GAE中,沒有直接支持多對多關系的功能,而是提供了對相關關系使用鍵集的功能。 因此在AI中將在B中維護記錄鍵集。
現在的問題是-如何查詢屬於給定類型A的對象並符合特定條件的類型B的對象? 在普通的SQL中,我會這樣做:
select B.*
from
B inner join A
on B.A_ID=A.ID
where B.property0=criteria1
and B.property1=criteria2 ...
and ...
但是因為我不能加入,所以我需要做一些類似的事情
select B.*
from B
where B.A_ID in ( ... )
and B.property0=criteria1
and B.property1=criteria2 ...
and ...
因此,由於ID的數量,查詢本身可能會很長。
有什么更好的辦法嗎?
如果重構關系映射,則可以得到更好的查詢。 與其在A中存儲一組鍵,不如在B中存儲一組鍵。然后,您可以使用
select * from B where a_id = {idOfRelevantA} and property0 = {criterion0} and property1 = {criterion1}...
這樣,您可以避免in
運算符創建的多個查詢。
另外,請注意: in
僅適用於30個或更少元素的列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.