我有以下查询,该查询根据标签(子代)值正确返回标签(父/根实体),但是,它在响应中将父实体重复了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
答案 0 :(得分:2)
似乎问题在于JPA的运行方式。以下页面可能会有所帮助:https://issues.apache.org/jira/browse/OPENJPA-2333
(我会发表评论,但声望点不够……)