JPA和谷歌应用程序引擎中的多对多关系 [英] 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
对B.A_ID = A.ID
其中B.property0 =标准1
和B.property1 =标准2 ...
和...
但是因为我不能做JOIN,所以我需要做一些类似于
的<$ (...)
和B.property0 = criteria1
和B
$ b其中B.A_ID .property1 = criteria2 ...
和...
所以查询本身可以非常长,因为ID的数量。
有没有更好的方法?
如果你重构你的关系映射,你可以得到更好的查询。您可以使用
select * from进行查询,而不是在A中存储一组键。 B where a_id = {idOfRelevantA} and property0 = {criterion0} and property1 = {criterion1} ...
这样可以避免运算符创建的多个查询。
另外,请注意: in
只适用于30个或更少元素的列表。
I have entities A and B, and A can have set of B. The same instance of B can belong to several A. So there is classical many-to-many relation here.
In GAE there is no direct support of many-to-many relations, instead they're offering an ability to use sets of keys for related relations. So in A I will maintain set of keys of records in B.
Now the problem is - how can I query for objects of type B belonging to given object of type A and matching certain criteria? In plain SQL I would do that like:
select B.*
from
B inner join A
on B.A_ID=A.ID
where B.property0=criteria1
and B.property1=criteria2 ...
and ...
but because I can not do JOIN then I need to do something like
select B.*
from B
where B.A_ID in ( ... )
and B.property0=criteria1
and B.property1=criteria2 ...
and ...
so the query itself can be very long because of amount of IDs.
Is there any better way?
If you refactor your relationship mapping you can get a better query. Instead of storing a set of keys in A, store a set of keys in B. Then you can query with
select * from B where a_id = {idOfRelevantA} and property0 = {criterion0} and property1 = {criterion1}...
This way you avoid the multiple queries that the in
operator creates.
Also, beware: in
will only work for a list of 30 elements or fewer.
这篇关于JPA和谷歌应用程序引擎中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!