title: Custom Query in Spring JPA Repository with Pagination
body: <p>I have tried implementing JPA Repository with Spring Boot it works fine.
  Now if i try to implement custom query in interface which extends JpaRepository using @Query Annotation it works fine returns List of beans.(using NamedQuery).
  Now when i try to use pagination for custom method/query it doesn't work.</p>
  
  <p>Code : </p>
  
  <p>Controller :</p>
  
  <pre><code>@RequestMapping("/custompages/{pageNumber}")
  public String getAllEmployeesUsingNamedQueryWithPaging(@PathVariable Integer pageNumber,Model model)
  {
      Page&lt;Employee&gt; page = employeeService.getAllEmployeesUsingNamedQueryWithPaging(pageNumber);
  
      System.out.println("current page "+page);
      System.out.println("current page content"+page.getContent());
  
       int current = page.getNumber() + 1;
      int begin = Math.max(1, current - 5);
      int end = Math.min(begin + 10, page.getTotalPages());
  
      model.addAttribute("empList", page.getContent());
      model.addAttribute("empPages", page);
      model.addAttribute("beginIndex", begin);
      model.addAttribute("endIndex", end);
      model.addAttribute("currentIndex", current);
  
      return "employeeWorkbench";
  }
  </code></pre>
  
  <p>Service </p>
  
  <pre><code>@Override
  public Page&lt;Employee&gt; getAllEmployeesUsingNamedQueryWithPaging(Integer  
  pageNumber) {
  
      PageRequest pageRequest =
              new PageRequest(pageNumber - 1, PAGE_SIZE, 
      Sort.Direction.ASC, "id");
      return   
  employeeDao.getAllEmployeesUsingNamedQueryWithPaging(pageRequest);
  }
  </code></pre>
  
  <p>Dao</p>
  
  <pre><code>@Transactional
  public interface EmployeeDao  extends JpaRepository&lt;Employee, Long&gt;{
  
  @Query(name="HQL_GET_ALL_EMPLOYEE_BY_ID")//Works Fine
  public List&lt;Employee&gt; getEmpByIdUsingNamedQuery(@Param("empId") Long
  empId);     
  
  @Query(name="HQL_GET_ALL_EMPLOYEE") //throws exception
  public Page&lt;Employee&gt; getAllEmployeesUsingNamedQueryWithPaging(Pageable     
  pageable);  
  }
  </code></pre>
  
  <p>NamedQuery </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 
  3.0//EN"  
  "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
  
  &lt;query name="HQL_GET_ALL_EMPLOYEE"&gt;from Employee&lt;/query&gt;
  
  &lt;query name="HQL_GET_ALL_EMPLOYEE_BY_ID"&gt;from Employee where id = 
  :empId&lt;/query&gt;
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <blockquote>
    <p>Exception : java.lang.IllegalArgumentException: Type specified for
    TypedQuery [java.lang.Long] is incompatible with query return type
    [class com.mobicule.SpringBootJPADemo.beans.Employee]</p>
  </blockquote>
  
  <p>I just want to have pagination functionality provided by Spring JPA Repository for custom methods and query also.
  How can I achieve this?</p>