Affects: \<2.7.0>


Hello!

I've tried to make pageable request on JpaRepository with sort by argument, which led to PropertyReferenceException

At first glance behavior was unexpected to me, since I declared @Column annotation on field with name property, and I was expecting a query, generated by pageable request, to use this name as sort parameter in request, but this didnt' happen, since actually on query creation spring init PropertyPath, which tries to read a non-existing field from @Entity annotated class owner, which leads to this exception.

I consider this a bug since user expects name declared in @Column annotation to match query requests.

Example code:

package pckg;

import javax.persistence.*;

@Entity 
@Table(name = "example")
class Example {

    @Column(name = "id")
    public int id;

    @Column(name = "old")
    public int old;
}

package repo;

import org.springframework.data.jpa.repository.JpaRepository;
import pckg.Example;

public interface ExampleRepository extends JpaRepository<Example, Int> {
    Example findById(long id);
}
package controller;

import repo.ExampleRepository
import pckg.Example

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.{Page, PageRequest, Sort}
import org.springframework.web.bind.annotation.{GetMapping, RequestParam, RestController}

class ExampleController {

  @Autowired
  private var repo: ExampleRepository = _

  @GetMapping(value = Array("/example"))
  def getPage(@RequestParam(value = "page",   defaultValue = "0")  page:   String,
              @RequestParam(value = "size",   defaultValue = "10") size:   String,
              @RequestParam(value = "sort",   defaultValue = "id") sortBy: String) Array[Example] = try {
    val pageNumber = page.toInt
    val requestedSize = size.toInt
    val sort = Sort.by(sortBy)

    val pageable = PageRequest.of(pageNumber, requestedSize, sort)
    val resultPage: Page[Example] = repo.findAll(pageable)

    resultPage.getContent.asScala.toArray
  } catch {
    case e: Throwable => Array.empty
  }
}
import org.springframework.boot.{CommandLineRunner, SpringApplication}
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class App {}

object App {
  def main(args: Array[String]): Unit = {
    SpringApplication.run(classOf[App], args: _*)
  }
}
spring.datasource.url=jdbc:postgresql://localhost:5432/example
spring.datasource.username=example
spring.datasource.password=example
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

At this point I try to execute localhost:8080/example?sort=old which works fine And after that I modify Example class:

package pckg;

import javax.persistence.*;

@Entity 
@Table(name = "example)
class Example {

    @Column(name = "id")
    public int id;


//    @Column(name = "old")
    @Column(name = "bold")
    public int old;
}

And then execute localhost:8080/example?sort=bold, which leads to an exception

Comment From: rstoyanchev

Your question most likely for the Spring HATEOAS project, but this is the Spring Framework issue tracker.