Skip to content

Pageable Always Adding From Entity to Sort Field Name [DATAJPA-1332] #1655

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

Closed
spring-projects-issues opened this issue Apr 23, 2018 · 12 comments
Assignees
Labels
in: query-parser Everything related to parsing JPQL or SQL status: feedback-provided Feedback has been provided type: bug A general bug

Comments

@spring-projects-issues
Copy link

Vijay opened DATAJPA-1332 and commented

I am having the exact issue that is mentioned on this link https://jira.spring.io/browse/DATAJPA-726
It is mentioned as solved there, but I am getting the same issue, and a search on stackoverflow shows that the problem persists. I am using SpringBoot 1.5.12.

Basically, I am doing a pagination on JOIN query as follows

@Query(Select DISTINCT new com.api.dto.FilterDTO(c.id, p.id, CASE WHEN item.dateDue IS NOT NULL THEN item.dateDue ELSE p.dateDue END AS newDateDue) FROM Customer c join c.productOrder p JOIN p.items item ")
List<FilterDTO> findDates(Pageable pageable);

And my page request is

Pageable page = new PageRequest(0, 20, Direction.DESC, "newDateDue");

However, when i run this I keep getting the error

could not resolve property: newDateDue of: com.api.beans.Customer

Evidently, it is searching for the sort field (i.e. newDateDue) in the Customer entity where it is not present.

So my question is

  1. Is Pagable not applicable when a JOIN is present ?
  2. Then is the only alternative to use native sql query ? But the link provided above says, this has been solved and it works for Spring Data JPA - I would like to continue using Spring Data JPA for this if it works - please help.

Thanks.

Note:There are other queries too on stackoverflow with similiar problems but no solutions. Links are as below


1 votes, 4 watchers

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

Do you have an executable sample, that shows the problem?

@spring-projects-issues
Copy link
Author

Vijay commented

I don't have a separate sample just for this - but can create one if you feel this shouldn't be happening. I was wondering if aliases are supported in first places in pagination.

 

Do let me know, so that I can create a sample for you. Thanks

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

They are. However, without you specifying what dateDue is supposed to refer to, how are we supposed to know whether it's supposed to be bound to c, p or item? Using item.dueDate should work, I guess

@spring-projects-issues
Copy link
Author

Vijay commented

Hello again,

I am creating an alias field, newDateDue (I have renamed the alias field name since it was probably confusing to have the same alias name as the field names) from two columns & I wanted to sort by this alias field newDateDue. Since it is an alias field, I can't specify either c,p or item.

(The Use case is that one of item or p can have a dueDate and I want to combine the dueDate from the 2 tables and sort by the new aliased field newDueDate).

My thinking is that if order by alias is possible without specifying table names (as in here https://coderanch.com/t/545957/databases/JPQL-SELECT-queries-field-aliases] then it probably is possible here too. 

Hope I am clear now.

Thanks

@spring-projects-issues
Copy link
Author

Vijay commented

Hello, could you please guide on this. Thanks

@spring-projects-issues
Copy link
Author

Vijay commented

I resolved this through Criteria Queries - which are powerful, although tough to learn.

Criteria Query cut through this (seemingly difficult query using  spring data) like a knife through butter. 

Thanks

@spring-projects-issues
Copy link
Author

KnyGoo commented

Vijay

Can you provide an example please?

 

I have the same issue...

@spring-projects-issues
Copy link
Author

Alexander commented

Hi all! The issue is still valid for at least Spring Data Jpa 2.1.x, 2.2.x

I've created a simple project to reproduce the problem - https://github.com/baranchikovaleks/data-jpa-sort

@spring-projects-issues spring-projects-issues added the type: bug A general bug label Dec 30, 2020
@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided in: query-parser Everything related to parsing JPQL or SQL labels Dec 30, 2020
@derylspielman
Copy link

I too am facing this exact same issue. It is tacking on the alias "f." of the main entity instead of first searching for alias names in the list of selects. This works if I use "cl.name" as a sort field, but fails if I use alias "clientName".

Caused by: org.hibernate.QueryException: could not resolve property: clientName of: com.example.FtmRemoteFileSetting [SELECT NEW com.example.FtmRemoteFileSettingDto(f.fileName, cl.name AS clientName) FROM com.example.FtmRemoteFileSetting f LEFT JOIN f.client cl order by f.clientName asc]
        at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
        at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:220)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162)
        at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604)
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716)

gregturn added a commit that referenced this issue Mar 22, 2023
@gregturn
Copy link
Contributor

Looks like this issue will be resolved via #2863.

@gregturn gregturn linked a pull request Mar 22, 2023 that will close this issue
gregturn added a commit that referenced this issue Apr 5, 2023
If a projection of either an HQL or JPQL query is aliased, do NOT prefix the FROM clause's alias prefix to any relevant applied sorting. Same for function-based order by arguments.

Resolves #2863.
Related: #2626, #2322, #1655.
Original pull request: #2865.
@gregturn
Copy link
Contributor

gregturn commented Apr 5, 2023

If you check 39e12ea, you should see that aliasing is now properly handled in sorts, and that the query at the top is included as a test case.

Feel free to check out Spring Data JPA 3.1.0-SNAPSHOT and test it out for yourself.

@gregturn gregturn self-assigned this Apr 5, 2023
@gregturn
Copy link
Contributor

Closing this as resolved by the earlier message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: query-parser Everything related to parsing JPQL or SQL status: feedback-provided Feedback has been provided type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants