Repository: spring-projects/spring-data-jpa

Issue #1997: JpaSort.unsafe fails to sort Jsonb fields [DATAJPA-1701]
Created: 2020-03-24T14:28:21Z
C.Kandaurova opened DATAJPA-1701 and commented
Hi,
I'm using Postgres with a table, containing JSONB field.
I want to sort & filter via fields in jsonb object.
 
Here is working Postgres query
 
SELECT * FROM my_object order by description->>'en-us'
 
Here is a sample test for Spring repository:
 
 
@Entity
@Table(name = "my_object")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class MyObject {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
	
    @Type(type = "jsonb")
    @Column(name = "description")
    private Map<String, String> description;

    @Column(name = "sample")
    private String sample;
	
}

@Repository
public interface MyObjectRepository extends PagingAndSortingRepository<MyObject, Long> {
}

****
@Test
public void pagingTest() {	
	MyObject entry = new MyObject();
	Map<String, String> descr = new HashMap<>();
	descr.put("en-us", "sorcerer's stone");
	descr.put("en-gb", "philosopher's stone");
	entry.setDescription(descr);
	repository.save(entry);
	//some other test data
	
	Pageable p = PageRequest.of(1,10,
			JpaSort.unsafe(Sort.Direction.ASC,"description->>'en-us'"));
	Iterable<MyObject> all = repository.findAll(p);
	assert all != null;
}
 
 
Test fails with the following logs:
 
org.springframework.data.mapping.PropertyReferenceException: No property description->>'en-us' found for type MyObject!	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94)
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:382)
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:358)
	at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:311)
	at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:293)
	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:276)
	at org.springframework.data.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:606)
	at org.springframework.data.jpa.repository.query.QueryUtils.toOrders(QueryUtils.java:559)
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:697)
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:651)
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:443)
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:410)
 
 
Issue on stackoverflow https://stackoverflow.com/questions/45358010/spring-jpa-sorting-and-paging-with-postgresql-jsonb hints that this solution has been working some releases ago.
 
Other tests with JpaSort.unsafe also fail, for example:
 
Pageable p = PageRequest.of(1,10, JpaSort.unsafe(Sort.Direction.ASC,"LENGTH(sample)")); 
Iterable<MyObject> all = repository.findAll(p);
 
Is it still possible to query ignoring propertyType safety?
 
 

Affects: 2.2.5 (Moore SR5)

Duplicate issue #3563: Spring data JPA fails to sort with JSONB column
Created: 2024-08-07T06:16:30Z
I'm using Spring Data JPA (v3.0.10) to sort and paginate a table in Postgres.
I'm facing issue when I try to sort by JSONB column.
Pageable p = PageRequest.of(1,19, JpaSort.unsafe(Sort.Direction.ASC, "publicationSchedule->>'permissionDeadline'"));
Exception:
Request processing failed: org.springframework.data.mapping.PropertyReferenceException: No property 'publicationSchedule->>'permissionDeadline'' found for type 'Publication'] with root cause
org.springframework.data.mapping.PropertyReferenceException: No property 'publicationSchedule->>'permissionDeadline'' found for type 'Publication'
	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-3.2.4.jar:3.2.4]

Closing comment (2024-08-07T11:26:32Z): duplicate of: #1997