简体   繁体   English

JPA和Google App Engine中的多对多关系

[英]JPA and many-to-many relations in google app engine

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. 我有实体A和B,并且A可以有B的集合。B的相同实例可以属于几个A。因此这里存在经典的多对多关系。

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. 在GAE中,没有直接支持多对多关系的功能,而是提供了对相关关系使用键集的功能。 So in AI will maintain set of keys of records in B. 因此在AI中将在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? 现在的问题是-如何查询属于给定类型A的对象并符合特定条件的类型B的对象? In plain SQL I would do that like: 在普通的SQL中,我会这样做:

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. 因此,由于ID的数量,查询本身可能会很长。

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 与其在A中存储一组键,不如在B中存储一组键。然后,您可以使用

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. 这样,您可以避免in运算符创建的多个查询。

Also, beware: in will only work for a list of 30 elements or fewer. 另外,请注意: in仅适用于30个或更少元素的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM