Spring data paging JPA (limit and offset)

I want the user to specify the limit (returned size) and offset (the first record / index returned) in the query method

This is my class without any paging function My entity:

@Entity
public Employee {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="NAME")
    private String name;

    //getters and setters
}

My warehouse:

public interface EmployeeRepository extends JpaRepository<Employee,Integer> {

    @Query("SELECT e FROM Employee e WHERE e.name LIKE :name ORDER BY e.id")
    public List<Employee> findByName(@Param("name") String name);
}

My service interface:

public interface EmployeeService {

    public List<Employee> findByName(String name);
}

My service implementation:

public class EmployeeServiceImpl {

    @Resource
    EmployeeRepository repository;

    @Override
    public List<Employee> findByName(String name) {
        return repository.findByName(name);
    }
}

Now I'm trying to provide paging capabilities that support offset and restriction My entity class remains unchanged

My new repository has pageable parameters:

public interface EmployeeRepository extends JpaRepository<Employee,Integer> {

    @Query("SELECT e FROM Employee e WHERE e.name LIKE :name ORDER BY e.id")
    public List<Employee> findByName(@Param("name") String name,Pageable pageable);
}

My new service interface has two additional parameters:

public interface EmployeeService {

    public List<Employee> findByName(String name,int offset,int limit);
}

My "new" service implementation:

public class EmployeeServiceImpl {

    @Resource
    EmployeeRepository repository;

    @Override
    public List<Employee> findByName(String name,int limit) {
        return repository.findByName(name,new PageRequest(offset,limit);
    }
}

This is not what I want Pagerequest specifies the page and size (page and page size) Now the specified size is exactly what I want, but I don't want to specify the starting page #, I want the user to specify the starting record / index I want something similar

public List<Employee> findByName(String name,int limit) {
    TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.name LIKE :name ORDER BY e.id",Employee.class);
    query.setFirstResult(offset);
    query.setMaxResults(limit);
    return query.getResultList();
}

In particular, the setfirstresult () and setmaxresult () methods But I can't use this method because I want to use the employee repository interface (or is it actually better to define queries through entitymanager)? Anyway, is there a way to specify the offset without using entitymanager? Thank you in advance!

Solution

You may not be able to use spring data JPA to this If the offset is very small, you may delete the first X statements from the query after retrieval

Otherwise, you can define the page size as an offset, starting from page 1

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>