Skip to content

Count query removes distinct phrase if from part of JPA query contains newline character  #2341

Closed
@kaliberr44

Description

@kaliberr44

It seems that there is newline character after the entity alias name in FROM part of the query before the JOIN the distinct is removed from the count.

Incorrect behavior:

@Query("""
    SELECT DISTINCT entity1
    FROM Entity1 entity1 
    LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key""")

returns

select count(entity1) FROM Entity1 entity1
LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key

Correct behaviour:

@Query("""
    SELECT DISTINCT entity1
    FROM Entity1 entity1 LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key""")

returns

select count(DISTINCT entity1) FROM Entity1 entity1 LEFT JOIN Entity2 entity2 ON entity1.key = entity2.key

From testing if the FROM and JOIN are in the same line, and WHERE is new line then this incorrect behavior also occurs.

For now I have solved it with countProjection = "distinct entity1".

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: query-parserEverything related to parsing JPQL or SQLtype: bugA general bug

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions