Skip to content

CountQuery fails to be auto generated when not using explicit alias [DATAJPA-1737] #2032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue May 29, 2020 · 2 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL type: bug A general bug

Comments

@spring-projects-issues
Copy link

Jonas Tänzer opened DATAJPA-1737 and commented

Consider the following repository: 

public interface BookErrorRepository extends Repository<BookError, Long> {

    @Query("FROM BookError b WHERE portal = :portal AND date >= :startDate AND date <= :endDate" +
           " AND (:search IS NULL OR keyword LIKE :search%)" +
           " AND (:state IS NULL OR errors = :#{#state?.shortMessage})")
    Page<BookError> findByPortalInDateRange(@Param("portal") Portal portal,
            @Param("startDate") LocalDate startDate,
            @Param("endDate") LocalDate endDate,
            @Param("search") Optional<String> search,
            @Param("state") Optional<State> state,
            Pageable pageable);

}

When changing this to 

FROM BookError WHERE portal [...]

the attached exception occurs on startup.

At first it seems like an issue with the HQL parser, but upon further inspection it seems that spring-data-jpa doesn't generate the count query correctly, because it recognizes WHERE as the alias. 

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: WHERE near line 1, column 14 [select count(WHERE) FROM com.example.project

This isn't a huge issue issue of course, but I figured I'd report to at least receive feedback anyway.

 

 


Attachments:

@jmax01
Copy link

jmax01 commented May 7, 2021

My native query example:

Fails:

    String FIND_BY_ALL_ATTRIBUTES_QUERY = """

            SELECT
              *
            FROM
              #{#entityName}
            WHERE
              attributes @> CAST(?1 as jsonb)

            """;

Resulting bad query:

SELECT
   COUNT(WHERE)  
FROM
  someTable
WHERE
  attributes @> CAST(?1 as jsonb)

Adding the alias works:

    String FIND_BY_ALL_ATTRIBUTES_QUERY = """

            SELECT
              *
            FROM
              #{#entityName} t
            WHERE
              attributes @> CAST(?1 as jsonb)

            """;

@schauder schauder added the in: query-parser Everything related to parsing JPQL or SQL label Jan 17, 2022
@gregturn gregturn self-assigned this Mar 20, 2023
gregturn added a commit that referenced this issue Mar 20, 2023
In the event of a Hibernate query where there is no explicit alias in the FROM clause, the query parser should be able to fashion a temporary alias and use it for counting.

Resolves #2032.
@gregturn
Copy link
Contributor

Part of this query's issue is that the LIKE clause details should be wrapped inside single-quotes. As for the rest, we're working up a solution to handle missing aliases.

@gregturn gregturn linked a pull request Mar 22, 2023 that will close this issue
@gregturn gregturn added this to the 3.1 RC1 (2023.0.0) milestone Mar 22, 2023
klajdipaja pushed a commit to klajdipaja/spring-data-jpa that referenced this issue Mar 24, 2023
In the event of a Hibernate query where the FROM clause has no alias, the query parser should use "__" as a stand-in alias.

Resolves spring-projects#2032.
Related: spring-projects#2220.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: query-parser Everything related to parsing JPQL or SQL type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants