Skip to content

Broken count query for native queries with table alias #2773

Closed
@hpoettker

Description

@hpoettker

The following repository currently does not work with MySQL:

public interface SomeRepository extends CrudRepository<SomeEntity, Long> {

  @Query(
      value = "SELECT * FROM table_name some_alias",
      nativeQuery = true
  )
  Page<SomeEntity> search(Pageable pageable);

}

The repository worked fine with Spring Data JPA 2.6.3, has been broken since 2.6.4, and is still broken in 2.7.7.

The log on failure is

Hibernate: SELECT * FROM table_name some_alias ?
Hibernate: select count(some_alias) FROM table_name some_alias
WARN --- o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22
ERROR --- o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'some_alias' in 'field list'

The problem is the count query that should be select count(*) FROM table_name some_alias with * instead of the alias as parameter to COUNT.

The regression seems to have been introduced with #2389 as the PR contains a test case for the erroneous behavior.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions