Spring Boot JPA Like query

Viewed 349

I'm trying toimplement a search function for my table of users. I want to make a JpaRepository method that will give me a pageable list of my users that have their emails containing a given String, how can I do that? This is whatIhave tried The repository:

    @Repository
public interface UserRepository extends JpaRepository<User, Long> { 
    Page<User> findByEmailContaining(String email, Pageable pageable);  
}

The servive:

public Page<User> findPaginatedEmail(int pageNo, int pageSize, String sortField, String sortDirection, String text) {
    Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortField).ascending() :
        Sort.by(sortField).descending();
    Pageable pageable = PageRequest.of(pageNo - 1, pageSize, sort);
    return this.userRepository.findByEmailContaining(text, pageable);
}

Sometimes it works and populates my table and sometimes it doesn't. What is the problem? How do I implement it?

It works fine but after a time it simply breaks I dont know why, then it wont find anything

2 Answers

what you are looking for is:

public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {

    List<Product> findAllByPrice(double price, Pageable pageable);
}

You just add one extra function parameter Pageable and also you need extend your Repository class with PagingAndSortingRepository instead of JpaRepository

Pagination example

There are many approaches, in case here it is been used the SimpleJpaRepository calling the findAll Method with parameters Specification and Pageable, returning the Page interface.

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;

    @GetMapping(path = "/pageable")
    public Page<?> getCountries(@RequestParam(name = "countryName", required = false) String countryName,
            @PageableDefault(page = 0, size = 20) @SortDefault.SortDefaults({
                    @SortDefault(sort = "id", direction = Sort.Direction.ASC) }) Pageable pageable)
    {
        
    }

My Repository interface :

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 

public interface CountryRepository extends JpaRepository<Country, Long>, JpaSpecificationExecutor<Country> { 
}

In case of having dynamic queries i use :

org.springframework.data.jpa.domain.Specification<T>

the spring data jpa :

org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>

Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);


org.springframework.data.jpa.repository.support.SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID>

    public Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable) {

        TypedQuery<T> query = getQuery(spec, pageable);
        return isUnpaged(pageable) ? new PageImpl<T>(query.getResultList())
                : readPage(query, getDomainClass(), pageable, spec);
    }

the repository CountryRepository calling the method findAll(@Nullable Specification spec, Pageable pageable) returns the Page

Related