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

How to limit the return results in a non-declarative query? #3124

Closed
Mpp1486356697 opened this issue Aug 23, 2023 · 3 comments
Closed

How to limit the return results in a non-declarative query? #3124

Mpp1486356697 opened this issue Aug 23, 2023 · 3 comments
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@Mpp1486356697
Copy link

Mpp1486356697 commented Aug 23, 2023

I want to be the same “
Collection findByLastname(String lastname, Class type);

,But it failed.
public interface PersonDao extends JpaRepository<Person,Long>, JpaSpecificationExecutor
,QuerydslPredicateExecutor {

<C> Page<C> findAll(Specification<Person> spec, Pageable pageable,Class<C> c);
<C> Page<C>  findAll(Predicate predicate, Pageable pageable,Class<C> c);

}
Using Querydsl to construct conditions is very convenient, but it does not allow limiting the return results - I don't want to use it “new JPAQueryFactory(em).select(QPerson.person.name).where(....,

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 23, 2023
@mp911de
Copy link
Member

mp911de commented Aug 23, 2023

Have you seen the findBy method that allows limiting?

List<User> results = repo.findBy(predicate, query -> query.limit(10).all());

Generally, Pageable allows limit results to one page if you're looking for a simpler API.

@mp911de mp911de closed this as completed Aug 23, 2023
@mp911de mp911de added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 23, 2023
@Mpp1486356697
Copy link
Author

Mpp1486356697 commented Aug 24, 2023

Have you seen the findBy method that allows limiting?

List<User> results = repo.findBy(predicate, query -> query.limit(10).all());

Generally, Pageable allows limit results to one page if you're looking for a simpler API.

Yes, you are correct.
Why doesn't findBy behave like findAllByName? “select p1_0.name ”
But instead "p1_0.id,p1_0.age,p1_0.name,p1_0.phone_id "
Or rather, what should I do then?

 personDao.findBy(Example.of(new Person("lrb",null,null)),
x -> x.as(PersonOnlyName.class).page(PageRequest.of(1, 1)));
// Hibernate: select p1_0.id,p1_0.age,p1_0.name,p1_0.phone_id from person p1_0 where p1_0.name=? limit ?,?
// Hibernate: select p1_0.id,p1_0.name from phone p1_0 where p1_0.id=?
// Hibernate: select count(p1_0.id) from person p1_0 where p1_0.name=?
personDao.findBy(Example.of(new Person("lrb",null,null)),
x -> x.project("name").page(PageRequest.of(1, 1)));
// Hibernate: select p1_0.id,p1_0.age,p1_0.name,p1_0.phone_id from person p1_0 where p1_0.name=? limit ?,?
// Hibernate: select count(p1_0.id) from person p1_0 where p1_0.name=?
Page lrb = personDao.findAllByName("lrb", PageRequest.of(1, 1), PersonOnlyName.class);
// Hibernate: select p1_0.name from person p1_0 where p1_0.name=? limit ?,?
// Hibernate: select count(p1_0.id) from person p1_0 where p1_0.name=?

@mp911de
Copy link
Member

mp911de commented Aug 24, 2023

findBy projections use fetch graphs instead of tuple queries. I think that is something we can improve on, see also #2600.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

3 participants