基于子实体的 属性 值搜索实体的 JPA 标准? (重复连接)
JPA criteria to search for entities based on property values of child entities? (Duplicate joins)
我有以下查询,它根据标签(子)值正确返回标签(parent/root 实体),但是,它重复父实体 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)
会消除重复项,并且似乎是推荐的方法,基于这个公认的答案:
看来问题可能出在 JPA 的运行方式上。这是一个可能有帮助的页面:https://issues.apache.org/jira/browse/OPENJPA-2333
(我会评论但没有足够的声望点...)
我有以下查询,它根据标签(子)值正确返回标签(parent/root 实体),但是,它重复父实体 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)
会消除重复项,并且似乎是推荐的方法,基于这个公认的答案:
看来问题可能出在 JPA 的运行方式上。这是一个可能有帮助的页面:https://issues.apache.org/jira/browse/OPENJPA-2333
(我会评论但没有足够的声望点...)