【发布时间】:2011-11-07 12:21:34
【问题描述】:
我有实体A和B,A可以有B的集合。B的同一个实例可以属于多个A。所以这里有经典的多对多关系。
在 GAE 中,没有直接支持多对多关系,而是提供了使用一组键来建立相关关系的能力。因此,在 A 中,我将在 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 的数量,查询本身可能会很长。
有没有更好的办法?
【问题讨论】:
标签: java google-app-engine jpa persistence many-to-many