Skip to content
New issue

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

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

Already on GitHub? # to your account

Problem with Pair sorting #72

Open
undefined-user-ctrl opened this issue May 3, 2018 · 3 comments
Open

Problem with Pair sorting #72

undefined-user-ctrl opened this issue May 3, 2018 · 3 comments

Comments

@undefined-user-ctrl
Copy link

When you have a Pair<Entity,Long>, and you try to sort by
.sorted(c -> c.getTwo());

The query generated is very close to expected, but has an ")" in the wrong position:

ORDER BY (SELECT COUNT(A) FROM Entity A WHERE B.id = A.entity.id ASC)

Maybe if an extra parentheses is placed around the column name, without the 'ASC', it may solve the problem, without much effort:

ORDER BY ((SELECT COUNT(A) FROM Entity A WHERE B.id = A.entity.id) ASC)

Or, as checking in SelectFromWhere class, it should be reversed:

queryState.queryString += (sortParams.isAscending ? " ASC" : " DESC");
if (sortParams.expr instanceof SubqueryExpression)
// Special handling of subquery parantheses
queryState.queryString += ")";

Don't know if it fits another scenarios.
Thanks

@undefined-user-ctrl
Copy link
Author

undefined-user-ctrl commented May 3, 2018

By the way, in my scenario its a Count.
If in the query the expression recieve an alias, it can be used in sort:

Original:

SELECT B, (SELECT COUNT(A) FROM entity A WHERE B.id = A.entity.id) as c FROM anotherEntity B ORDER BY (SELECT COUNT(A) FROM Entity A WHERE B.id = A.entity.id DESC)

Final:
SELECT B, (SELECT COUNT(A) FROM entity A WHERE B.id = A.entity.id) as c FROM anotherEntity B ORDER BY c DESC

@undefined-user-ctrl
Copy link
Author

In my case, i was able to use by sorting from column position (Count value).

query.sortedBy(x-> 2);

@my2iu
Copy link
Owner

my2iu commented May 3, 2018

I'm a bit busy at the moment, so I'll take a closer look next week if I can find the time. In the past, I've had all sorts of problems with sorting by subqueries using different JPA providers.

Would you be able to just use standard groups to get the same effect? Those operations are much more reliable and much faster in JPA and SQL than using subqueries.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants