Skip to content
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

SELECT HQL without entity alias results in SELECT COUNT(null) query #3744

Closed
kotelnik opened this issue Jan 13, 2025 · 1 comment
Closed
Assignees
Labels
type: bug A general bug

Comments

@kotelnik
Copy link

When writing e.g. this query:

@Query("SELECT id FROM Person")
Page<Long> findIdsPageable(Pageable pageable);

I get always only one page of data and I can see this in the log:

Hibernate: select p1_0.ID from Person p1_0 fetch first ? rows only
Hibernate: select count(null) from Person p1_0
2025-01-13 22:59:44,762 (main) WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] SQL Warning Code: -1003, SQLState: 01003
2025-01-13 22:59:44,763 (main) WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] warning: null value eliminated in set function

When using previous version of spring-data-jpa (3.3.7), I can see correct count(...) query and paging is working correctly:

Hibernate: select p1_0.ID from Person p1_0 fetch first ? rows only
Hibernate: select count(p1_0.ID) from Person p1_0

Is there something I should do differently to fix this issue?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 13, 2025
@mp911de mp911de self-assigned this Jan 14, 2025
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 14, 2025
@mp911de
Copy link
Member

mp911de commented Jan 14, 2025

For the time being, please update your query by adding an alias to the FROM Person clause updating it to SELECT p.id FROM Person p. While your query meets the HQL spec, it isn't compatible with JPQL, which requires aliases for entity names.

@mp911de mp911de added this to the 3.4.2 (2024.1.2) milestone Jan 14, 2025
@mp911de mp911de changed the title count(null) in pageable query with explicit SELECT column since 3.4.0+ SELECT HQL without entity alias results in SELECT COUNT(null) query Jan 14, 2025
mp911de added a commit that referenced this issue Jan 14, 2025
Revisit DISTINCT count queries when primary alias isn't set. HQL can handle such queries, JPQL and EQL transformers now fail properly.

See #3744
mp911de added a commit that referenced this issue Jan 14, 2025
Revisit DISTINCT count queries when primary alias isn't set. HQL can handle such queries, JPQL and EQL transformers now fail properly.

See #3744
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants