【问题标题】:JPA: query matching many-to-may properties of unrelated entitiesJPA:查询匹配不相关实体的多对可能属性
【发布时间】:2022-01-02 19:09:25
【问题描述】:

我有以下三个实体:

@Entity
public class Item {
    [...]
    @ManyToOne(optional = false) 
    @JoinColumn(nullable = false)
    private ItemDescriptor asset;
    [...]
}

@Entity
public class ItemDescriptor {
    [...]
    @ManyToMany(fetch = FetchType.LAZY)
    private Set<ContentPlan> contentPlans;
    [...]
}

@Entity
public class Tenant {
    [...]
    @ManyToMany
    private Set<ContentPlan> contentPlans;
    [...]
}

现在,我正在寻找一个 JPA 查询给我:

对于给定的租户 t,获取 i.asset.contentPlans 所在的所有项目 i t.contentPlans

我找到了几个 realted soultions,但没有一个能真正解决这个设置的问题。有人能指出我正确的方向吗?

【问题讨论】:

    标签: java jpa jakarta-ee


    【解决方案1】:

    我想出了这个:

    SELECT i FROM Item i, Tenant t WHERE i.asset.contentPlans IN (t.contentPlans) AND t = :tenant
    

    我想这应该是工作..?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 2014-12-30
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多