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
2.92k stars 1.39k forks source link

Redundant parameter in the Limit query #3242

Open sergey-morenets opened 7 months ago

sergey-morenets commented 7 months ago

Hi

Spring Data JPA introduced new Limit functionality to provide dynamic limiting. So if earlier we wrote the following query:

    List<Product> findTop3By();

    List<Product> findFirst3By();

and now we can use new approach:

List<Product> findBy(Limit limit);

But I noticed that resulting SQL in the first two queries was :

SELECT * from Product p1_0 fetch first ? rows only

And now it's:

SELECT * from Product p1_0 offset ? rows fetch first ? rows only

So why do we need additional parameter offset? I guess it's redundant and not needed here.

mp911de commented 7 months ago

This is because we convert by default Limit into Pageable(0, limit) and set the Query.setFirstResult(…) value implicitly.

Actually, Hibernate could omit that value as not setting the offset and setting offset zero should yield the same result.

quaff commented 7 months ago

I will try to fix it at hibernate side.

quaff commented 7 months ago

I've created https://github.com/hibernate/hibernate-orm/pull/7579 to fix this.

sergey-morenets commented 7 months ago

I've created hibernate/hibernate-orm#7579 to fix this.

Thank you, @quaff

quaff commented 4 weeks ago

I've created hibernate/hibernate-orm#7579 to fix this.

Thank you, @quaff

FYI, Hibernate team decline that.