Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JpaSort.unsafe fails to sort Jsonb fields [DATAJPA-1701] #1997

Closed
spring-projects-issues opened this issue Mar 24, 2020 · 4 comments
Closed
Assignees
Labels
in: core Issues in core support status: duplicate A duplicate of another issue

Comments

@spring-projects-issues
Copy link

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)

@xiaohulu
Copy link

xiaohulu commented Nov 8, 2022

2.7.5 still have this bug!

@gregturn gregturn self-assigned this Oct 5, 2023
@gregturn gregturn added status: duplicate A duplicate of another issue and removed type: bug A general bug labels Oct 5, 2023
@gregturn
Copy link
Contributor

gregturn commented Oct 5, 2023

#3172 duplicates this issue.

@gregturn gregturn closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2023
@abirami-els
Copy link

I'm facing the same issue. I'm trying to sort by JSONB column, but it's not working.

Pageable p = PageRequest.of(1,19, JpaSort.unsafe(Sort.Direction.ASC, "publicationSchedule->>'permissionDeadline'"));

@abirami-els
Copy link

Any fix for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants