Mindsect Team
Mindsect Team

Reputation: 2571

How to Use JPA setMaxResults() (LIMIT 20) Without Entity Manager?

I currently have the following JPA set-up:

com.example.entities.Employee
com.example.repository.EmployeeRepository
com.example.controllers.EmployeeController

The application settings are pointing to an SQL server and the the controller has endpoints (i.e. '/getEmployees') with no issues.

However, I would like to LIMIT the number of employees that are returning.

After using JPA, I am not using any entityManager code as many tutorials suggest.

Instead, I am using the following design pattern in the repository:

com.example.repository.EmployeeRepository

package com.example.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.example.entities.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

    @Query("SELECT "
        + "e.eid, "
        + "e.firstname, "
        + "e.lastname, "
        + "e.dept) "
        + "FROM Employee e")
    public List<Employee> getAllEmployees();

}

Below is a copy of the controller file for the endpoint:

com.example.controllers.EmployeeController

package com.example.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.entities.Employee;
import com.example.repository.EmployeeRepository;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeRepository empRepo;

    @RequestMapping(name = "/getAllEmployees")
    public @ResponseBody List<Employee> getAllEmployees() {
        return empRepo.getAllEmployees();
    }

}

As you can see, there is no mention of entityManager in these files and so it is become more of an abstract situation, where I am not sure where I would initiate the setMaxResults(20) method without explicitly using entityManager.

I've noticed that setMaxResults function is usually attached to a query object:

q.setMaxResults(n)

Any ideas?

Upvotes: 3

Views: 10106

Answers (2)

Hannoun Yassir
Hannoun Yassir

Reputation: 21182

How about using Pageable interface in Spring data?

package com.example.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable

import com.example.entities.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

    @Query("SELECT "
        + "e.eid, "
        + "e.firstname, "
        + "e.lastname, "
        + "e.dept) "
        + "FROM Employee e")
    public Page<Employee> getAllEmployees(Pageable pageable);

}

In your controller you can set how many items you'd like to get :

@RequestMapping(name = "/getAllEmployees")
public @ResponseBody List<Employee> getAllEmployees() {
    return empRepo.getAllEmployees(new PageRequest(0, 20)).getContent();
}

Upvotes: 4

Mike Adamenko
Mike Adamenko

Reputation: 3002

HQL doesn't support LIMIT feature, so you have 2 options here:

  • use native SQL query
  • use entityManager and setMaxResults

Upvotes: 4

Related Questions