I map the result of the following JPQL-Query directly to a SpecialCustomDto object instead of the used javax.persistency entity object MyEntity. But I do not know, how to access the COUNT(DISTINCT e.attributeB), which will be mapped to the SpecialCustomDto.
This is the query.
@Repository
public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
@Query("SELECT new com.test.SpecialCustomDto(e.attributeA, COUNT(DISTINCT e.attributeB)) as specialCustomDto "
+ "FROM MyEntity e WHERE 5 = specialCustomDto.count GROUP BY e.attributeA")
List<SpecialCustomDto> getSpecialCustomDtos();
}
As soon as I start the spring-boot application, Hibernate throws me following error:
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: as near line 1, column...
I don't know how to access the aggregated COUNT(DISTINCT e.attributeB) element of the newly created SpecialCustomDto. Without the additional WHERE-clause, the mapping works as expected.
new com.test.SpecialCustomDto(e.attributeA, COUNT e.attributeB)Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: COUNT near line 1,