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 throws exception for non-entity properties [DATAJPA-1643] #1946

Closed
spring-projects-issues opened this issue Dec 6, 2019 · 5 comments
Assignees
Labels
in: core Issues in core support status: duplicate A duplicate of another issue

Comments

@spring-projects-issues
Copy link

Milan Milanov opened DATAJPA-1643 and commented

I'm using the spring boot starter JPA version 2.2.1 RELEASE, which pulls the spring data JPA project with the same dependency. I'd like to use the specification + paging findAll method, but with a twist, that the sorting should be done based on a function, and not on a simple property.

Sort sort = JpaSort.unsafe("nlssort(entity_title, 'NLS_SORT = XGERMAN_DIN_AI')");

var activities = entityRepository.findAll(new EntitySpecification(...), PageRequest.of(0, 10, sort))

Running this code I get an exception saying that No property nlssort(entity found for type Entity!. The same question is asked here, and in fact the accepted answer actually works. The problem is that adding a query doesn't play along with a specification. Just for test i tried:

@Query("SELECT s FROM Entity s")
Page<Entity> findAll(Specification<Entity> spec, Pageable pageable);

Then the query is actually generated and executed, but all results are just sorted, nothing is filtered. Also i don't know how this would work since there are aliases, but nevertheless:

SELECT * 
FROM   (SELECT ...
               entity0_.entity_title        AS entity_title6_1_, 
               ...
        FROM   entities entity0_ 
        ORDER  BY nlssort(entity_title, 'NLS_SORT = XGERMAN_DIN_AI') ASC) 
WHERE  ROWNUM <= ? 

Affects: 2.2.3 (Moore SR3)

@incepter
Copy link

Hello,

Did you find a workaround for this ? Im also stick with this:

org.springframework.data.mapping.PropertyReferenceException: No property (CASE WHEN consition THEN value END) found for type BatchCollectOperationItem!

Thanks,

@incepter
Copy link

incepter commented Feb 22, 2021

We ended up doing something similar to this:

public static Specification<Example> getOrderSpecification() {
        return (root, cq, cb) -> {
            final Expression<Object> orderCase = cb.selectCase(root.get(Example_.PROCESSING_STATUS))
                    .when(ExampleStatus.PENDING.name(), 0)
                    .when(ExampleStatus.ERROR.name(), 1)
                    // ...
                    .otherwise(99);
            final Order order = cb.asc(orderCase);
            cq.orderBy(order);
            return null;
        };
    }

@voleum voleum mentioned this issue Dec 16, 2021
4 tasks
@xiaohulu
Copy link

xiaohulu commented Nov 8, 2022

2.7.5 also have this bug

@brandonfl
Copy link

brandonfl commented Feb 22, 2024

Hello,

The problem is still present in version 2.7.6
Any idea on how to disable the verification or something similar ?

I dont find any workaround, the solution from @incepter dont work when we use it with PostgreSQL DISTINCT

Thanks in advance,
Regards,
BrandonFL

@mp911de mp911de added type: enhancement A general enhancement and removed type: bug A general bug labels Jan 27, 2025
@mp911de
Copy link
Member

mp911de commented Jan 27, 2025

When using Specifications, order-by must be an expression and be translated into such. This will be addressed via #3172 so closing this one as duplicate.

@mp911de mp911de closed this as completed Jan 27, 2025
@mp911de mp911de added status: duplicate A duplicate of another issue and removed type: enhancement A general enhancement labels Jan 27, 2025
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

6 participants