spring boot 3 - hibernate 6.1 - 使用构造函数表达式查询按错误分组

问题描述 投票:0回答:1

迁移到 Spring Boot 3.1.4 后,该查询不再起作用。

@Query("""
        SELECT new com.xx.xx.xx.MyClassWrapper(
        customClass1,
        max(lpd.fieldA),
        max(lpd.fieldB),
        max(lpd.fieldC),
        max(lpd.fieldD)
        )
        FROM customClass2 AS lpd
        JOIN customClass1 AS customClass1 ON lpd.fieldName = customClass1 .id
        GROUP BY customClass1 
        """)

我收到此错误:

[SqlExceptionHelper.java:138] ERROR: column "xx.name_of_other_column" must appear in the GROUP BY clause or be used in an aggregate function
jpa spring-data-jpa jpql hibernate-6.x
1个回答
0
投票

更改为基于界面的投影解决了我的问题。 它应该看起来像这样:

@Query("""
        customClass1 as nameAttribute1,
        max(lpd.fieldA) as nameAttribute2,
        max(lpd.fieldB) as nameAttribute3,
        max(lpd.fieldC) as nameAttribute4,
        max(lpd.fieldD) as nameAttribute5
        )
        FROM customClass2 AS lpd
        JOIN customClass1 AS customClass1 ON lpd.fieldName = customClass1.id
        GROUP BY nameAttribute1 
        """)

还有界面:

public interface IndemniteDotaLimiteResponsabiliteWrapper {

     String getNameAttribute1();
     Double getNameAttribute2();
     Integer getNameAttribute3();
     Double getNameAttribute4();
     Integer getNameAttribute5();
}
© www.soinside.com 2019 - 2024. All rights reserved.