5

Using Spring Boot and Spring Data. Using JpaSort.unsafe but getting Exception. Is this a known Spring issue? Hibernate/JPA issue? Thanks.

Maven:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>

Entity:

public class Simple {
    @Id
    private Long id;
    private String firstName;
    ...
}

Repository method:

public List<Simple> findAll(Sort sort);

SpringTestClass:

    list = simpleRepository.findAll(JpaSort.unsafe("LENGTH(firstName)"));
    assertEquals("firstName", list.get(0).getFirstName());

Exception:

org.springframework.data.mapping.PropertyReferenceException: No property LENGTH(firstName) found for type Simple!
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Oliver Chua
  • 73
  • 1
  • 6

2 Answers2

3

In my case, adding @Query to repository class method helps solve this issue. I don't understand why. You can try if it helps you

@Query("SELECT s FROM Simple s")
inv
  • 46
  • 1
  • 4
1

This post here says you have to put parentheses around the string you are passing in. Maybe it applies here.

How to sort projection by alias from SELECT clause in Spring Data JPA with pagination?

awhig
  • 507
  • 1
  • 7
  • 13