spring boot 3 - hibernate 6.1 - group by error with constructor expression query

43 views Asked by At

After migration to spring boot 3.1.4, this query does not work anymore.

@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 
        """)

I get this error:

[SqlExceptionHelper.java:138] ERROR: column "xx.name_of_other_column" must appear in the GROUP BY clause or be used in an aggregate function
1

There are 1 answers

0
Amir Choubani On BEST ANSWER

Changing to interface based projection solved the problem for me. It should looks something like this:

@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 
        """)

And the interface :

public interface IndemniteDotaLimiteResponsabiliteWrapper {

     String getNameAttribute1();
     Double getNameAttribute2();
     Integer getNameAttribute3();
     Double getNameAttribute4();
     Integer getNameAttribute5();
}