spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
3.02k stars 1.42k forks source link

spring data jpa findAll NULLSFIRST does not work #3658

Closed Jon3012 closed 2 weeks ago

Jon3012 commented 2 weeks ago

Spring data jpa does not generate hql with NullsFirst .

I am using spring boot version 3.3.4 and trying to retrieve employee (for example) from postgres database using spring data jpa

Sort.Order order = new Sort.Order(Sort.Direction.ASC, "age", Sort.NullHandling.NULLS_FIRST)

employeeRepository.findAll(Sort.by(order));

while testing , generated query does not contain order by nulls first.

select id, name from employee order by age nulls first is missing in the generated query.

But if i add jpa.properties.hibernate.order_by.default_null_ordering = first in application.yml, then the generated query contains order by nulls first. and it works with nulls_last too. But i need to generate NullsFirst and Nulls last on the columns mentioned on Sort order like the example given .

How to fix this issue where jpa does not generate NullsFirst or NullsLast based on Sort order Null Handling (First and Last) example.. Sort.Order order = new Sort.Order(Sort.Direction.ASC, "age", Sort.NullHandling.NULLS_FIRST)

mp911de commented 2 weeks ago

This is a duplicate of #1280. Null precedence is being supported with JPA 3.2+.