【问题标题】:JPA and many-to-many relations in google app engine谷歌应用引擎中的 JPA 和多对多关系
【发布时间】: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


    【解决方案1】:

    如果重构关系映射,您可以获得更好的查询。不是在A中存储一组键,而是在B中存储一组键。然后你可以查询

    select * from B where a_id = {idOfRelevantA} and property0 = {criterion0} and property1 = {criterion1}...
    

    这样可以避免in 运算符创建的多个查询。

    另外,请注意:in 仅适用于 30 个或更少元素的列表。

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 2012-07-15
      • 2014-03-02
      • 1970-01-01
      相关资源
      最近更新 更多