我的Hibernate/jpql查询在加入时返回重复的值,我该如何避免
我有jpql查询,将查询的输出映射到特定对象
return em.createQuery(
"SELECT DISTINCT new org.some.great.project.Statuses(u.accountStatus, COUNT(u.accountStatus)) "
+ "FROM XUser u LEFT JOIN u.products p "
+ "WHERE p.id IN :productIds "
+ "GROUP BY u.accountStatus", Statuses.class)
.setParameter(PRODUCT_IDS, productIds)
.getResultList();
,并且一切都可以,直到Xuser
确实与product
表的关系确实存在。 如果它具有多个,则count(xx)
将添加其他记录。
我在Xuser
中的映射看起来像是按照
public class XUser {
//other
@ManyToMany
@JoinTable(
name = "XUser_product",
joinColumns = @JoinColumn(
name = "user_id",
referencedColumnName = "id"
),
inverseJoinColumns = @JoinColumn(
name = "product_id",
referencedColumnName = "id"
)
)
private Set<Product> products;
//other
}
我检查日志Hibernate(映射JPQL到lasterin sql)正在运行查询:
SELECT DISTINCT t.ACCOUNTSTATUS, COUNT(t.ACCOUNTSTATUS) FROM XUser x, XUSER_PRODUCT xp , PRODUCT p
WHERE x.ID = xp.XUSER_ID
AND xp.PRODUCT_ID = p.ID
AND p.ID IN (some nubers)
GROUP BY t.ACCOUNTSTATUS;
因此,如果我进行了一点修改,而不是返回count> count
有状态,我想返回所有我将获得的一切我获得
ID | 名称 | 帐户 | XUSER_ID | PRODUCT_ID | ID ID | NM | 价格 |
---|---|---|---|---|---|---|---|
1 | Peter | Dead | 1 | 12 | 12 | 测试 | 199 |
1 | Peter | Dead | 1 | 123 | 123 | Fruit | 999 |
2 | Tom | Ok | 2 | 98 | 98 Tom Ok 2 98 98 | Tabasco | 11 |
3 | Kate | Dead | 3 | 87 87 | 87 | Mars Mars | 2 |
您可以看到,我已经显示了peter
id = 1
两次。而且,如果此查询更改为以前的查询,我会看到
DEAD: 3 // and I need to have here 2
OK : 1
任何想法,暗示如何处理?
I have jpql query which maps output of query to specific object
return em.createQuery(
"SELECT DISTINCT new org.some.great.project.Statuses(u.accountStatus, COUNT(u.accountStatus)) "
+ "FROM XUser u LEFT JOIN u.products p "
+ "WHERE p.id IN :productIds "
+ "GROUP BY u.accountStatus", Statuses.class)
.setParameter(PRODUCT_IDS, productIds)
.getResultList();
And everything is ok, until XUser
does have only on relation to Product
table.
If it has more than one, count(xx)
will add additional record.
My mapping in XUser
looks like following
public class XUser {
//other
@ManyToMany
@JoinTable(
name = "XUser_product",
joinColumns = @JoinColumn(
name = "user_id",
referencedColumnName = "id"
),
inverseJoinColumns = @JoinColumn(
name = "product_id",
referencedColumnName = "id"
)
)
private Set<Product> products;
//other
}
If I check logs hibernate (maps jpql to followin sql) is running query:
SELECT DISTINCT t.ACCOUNTSTATUS, COUNT(t.ACCOUNTSTATUS) FROM XUser x, XUSER_PRODUCT xp , PRODUCT p
WHERE x.ID = xp.XUSER_ID
AND xp.PRODUCT_ID = p.ID
AND p.ID IN (some nubers)
GROUP BY t.ACCOUNTSTATUS;
So if I modify it a little, and instead of returning count
with status, I want to return everything I am getting
ID | NAME | ACCOUNTSTATUS | XUSER_ID | PRODUCT_ID | ID | NM | PRICE |
---|---|---|---|---|---|---|---|
1 | Peter | DEAD | 1 | 12 | 12 | test | 199 |
1 | Peter | DEAD | 1 | 123 | 123 | fruit | 999 |
2 | Tom | OK | 2 | 98 | 98 | tabasco | 11 |
3 | Kate | DEAD | 3 | 87 | 87 | mars | 2 |
So as you can see I have displayed Peter
with ID=1
two times. And If this query is changed to previous one I would see
DEAD: 3 // and I need to have here 2
OK : 1
Any ideas, hints how to handle it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论