【问题标题】:JPA criteria to search for entities based on property values of child entities? (Duplicate joins)基于子实体的属性值搜索实体的 JPA 标准? (重复连接)
【发布时间】:2020-03-09 15:17:40
【问题描述】:

我有以下查询,它根据标签(子)值正确返回标签(父/根实体),但是,它在响应中将父实体复制了 17 次(因为它有 17 个标签) .有什么想法我在这里做错了吗?

builder.and(
  builder.equal(root.join("labels").join("labelIdentity").get("key"), "owner"),
  builder.like(root.join("labels").get("value"), "bob")
);

更新

我已经根据https://issues.apache.org/jira/browse/OPENJPA-2333 尝试了以下方法,但这仍然返回 17 个重复的结果,而应该只返回一个:

final Join labels = root.join("labels", JoinType.INNER);
final Join labelIdentities = labels.join("labelIdentity", JoinType.INNER);

builder.and(
  builder.equal(labelIdentities.get("key"), "owner"),
  builder.like(labels.get("value"), "bob")
);

我相信使用 query.distinct(true) 会消除重复,并且似乎是推荐的方法,基于这个公认的答案:https://stackoverflow.com/a/11257160/12177456

【问题讨论】:

    标签: java hibernate jpa criteria


    【解决方案1】:

    看来问题可能出在 JPA 的运行方式上。这是一个可能有帮助的页面:https://issues.apache.org/jira/browse/OPENJPA-2333

    (我会发表评论,但没有足够的声望点...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2016-06-30
      • 2011-04-27
      • 2021-03-29
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      相关资源
      最近更新 更多