简体   繁体   English

Querydsl变换与分组依据并具有

[英]Querydsl transform with group by and having

I have a database table with products and want to find products with the same name via Querydsl 我有一个包含产品的数据库表,想通过Querydsl查找具有相同名称的产品

I have something like this: 我有这样的事情:

QProduct product = QProduct.product;
JPAQuery query = new JPAQuery(entityManager);

Map<String, List<Product>> = query.from(product)
            .transform(groupBy(product.name).as(list(product)));

Then I get a Map with Product names as key. 然后我得到一个以产品名称为键的地图。 To every name key I get a list with products having this name. 对于每个名称键,我都会列出具有该名称的产品。 Since I want to find name duplicates I need product lists greater than 1 only. 由于我想查找重复的名称,因此我只需要大于1的产品列表。 So I need a where clause with a nested SQL-Statement to ignore products with unique names: 因此,我需要一个带有嵌套SQL语句的where子句,以忽略具有唯一名称的产品:

SELECT * FROM Product p1
  WHERE (SELECT COUNT(*) FROM Product p2 WHERE p1.name=p2.name)>1 

How can I include this in the querydsl query above. 我如何在上面的querydsl查询中包括它。 Or is there another way to limit the product lists in the map to lists greater than 1 only. 还是有另一种方法将地图中的产品列表限制为仅大于1的列表。

Maybe there is a possibility using HAVING COUNT in querydsl? 也许有可能在querydsl中使用HAVING COUNT吗?

Thanks, Nick 谢谢,尼克

on the day one year later... someone had (almost) the same issue... and found a solution! 一年后的一天……有人(几乎)遇到了同样的问题……并找到了解决方案! =) =)

the sample below is not a solution to the question but a hint how to get a having statement into transform projection. 下面的示例不是解决问题的方法,而是提示如何将hading语句转换为变换投影。 I hope this helps at least someone! 我希望这至少对某人有帮助! :) :)

    QStagingData data = QStagingData.stagingData;
    List<StagingDataPlausiProjection> transform = new JPAQuery<>(getEntityManager())
            .select(Projections.bean(StagingDataPlausiProjection.class,
                    data.branch, data.account, data.costCenter, data.receiptMonth))
            .where(data.tenant.eq(tenant).and(data.bookingYear.eq(year)))
            .groupBy(data.branch, data.account, data.costCenter, data.receiptMonth)
            .having(data.value.sum().ne(BigDecimal.ZERO))
            .fetch();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM