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

Translate JpaSort.unsafe(…) used with Specifications to order-by Expression #3172

Open
Tracked by #3588
icerockq opened this issue Sep 25, 2023 · 3 comments · May be fixed by #3187
Open
Tracked by #3588

Translate JpaSort.unsafe(…) used with Specifications to order-by Expression #3172

icerockq opened this issue Sep 25, 2023 · 3 comments · May be fixed by #3187
Assignees
Labels
type: enhancement A general enhancement

Comments

@icerockq
Copy link

icerockq commented Sep 25, 2023

spring boot 2.7.2

In these two cases I use the following expression: PageRequest.of(0, 10, JpaSort.unsafe("LENGTH(name)")) and my model has name property.

For exmaple if I using default method findAll(Specifiocation, Pageable), then I will get exception:
org.springframework.data.mapping.PropertyReferenceException: No property 'unsafe expression' found for type 'Entity'

But if I using method with @Query annotation then I will get success query.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 25, 2023
@mp911de
Copy link
Member

mp911de commented Sep 25, 2023

Care to attach the full stack trace?

@icerockq
Copy link
Author

icerockq commented Sep 25, 2023

Sorry. Sure.

org.springframework.data.mapping.PropertyReferenceException: No property 'LENGTH(name)' found for type 'User'
	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:91) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:438) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:414) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:367) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:349) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:332) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:726) ~[spring-data-jpa-2.7.15.jar:2.7.15]
	at org.springframework.data.jpa.repository.query.QueryUtils.toOrders(QueryUtils.java:679) ~[spring-data-jpa-2.7.15.jar:2.7.15]
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:817) ~[spring-data-jpa-2.7.15.jar:2.7.15]
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:774) ~[spring-data-jpa-2.7.15.jar:2.7.15]
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:515) ~[spring-data-jpa-2.7.15.jar:2.7.15]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:530) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:286) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:640) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.29.jar:5.3.29]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:139) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.29.jar:5.3.29]
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:76) ~[spring-data-commons-2.7.15.jar:2.7.15]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.29.jar:5.3.29]
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.29.jar:5.3.29]
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.29.jar:5.3.29]
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.29.jar:5.3.29]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.29.jar:5.3.29]
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.29.jar:5.3.29]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.29.jar:5.3.29]
	at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) ~[spring-data-jpa-2.7.15.jar:2.7.15]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.29.jar:5.3.29]
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.29.jar:5.3.29]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.29.jar:5.3.29]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:241) ~[spring-aop-5.3.29.jar:5.3.29]
	at com.sun.proxy.$Proxy111.findAll(Unknown Source) ~[na:na]

@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 25, 2023
gregturn added a commit that referenced this issue Sep 26, 2023
@gregturn
Copy link
Contributor

gregturn commented Sep 26, 2023

I was able to reproduce this on the 2.7.x branch as well as the main branch.

gregturn added a commit that referenced this issue Oct 4, 2023
We have offered this method as a way for people to customize an ORDER BY clause beyond a simple property name. For example, someone could use "LENGTH(firstname)" as a function to order by the length of each row's lastname instead of ordering by the lastname itself.

By using the relevant parser and applying a different visitor, this commit make operational a feature that shows little evidence of ever having worked.

See #3172.
gregturn added a commit that referenced this issue Oct 4, 2023
We have offered this method as a way for people to customize an ORDER BY clause beyond a simple property name. For example, someone could use "LENGTH(firstname)" as a function to order by the length of each row's lastname instead of ordering by the lastname itself.

By using the relevant parser and applying a different visitor, this commit make operational a feature that shows little evidence of ever having worked.

See #3172.
gregturn added a commit that referenced this issue Oct 5, 2023
We have offered this method as a way for people to customize an ORDER BY clause beyond a simple property name. For example, someone could use "LENGTH(firstname)" as a function to order by the length of each row's lastname instead of ordering by the lastname itself.

By using the relevant parser and applying a different visitor, this commit make operational a feature that shows little evidence of ever having worked.

See #3172.
@gregturn gregturn linked a pull request Oct 5, 2023 that will close this issue
gregturn added a commit that referenced this issue Oct 5, 2023
We have offered this method as a way for people to customize an ORDER BY clause beyond a simple property name. For example, someone could use "LENGTH(firstname)" as a function to order by the length of each row's lastname instead of ordering by the lastname itself.

By using the relevant parser and applying a different visitor, this commit make operational a feature that shows little evidence of ever having worked.

See #3172.
gregturn added a commit that referenced this issue Oct 5, 2023
We have offered this method as a way for people to customize an ORDER BY clause beyond a simple property name. For example, someone could use "LENGTH(firstname)" as a function to order by the length of each row's lastname instead of ordering by the lastname itself.

By using the relevant parser and applying a different visitor, this commit make operational a feature that shows little evidence of ever having worked.

See #3172.
@mp911de mp911de added type: enhancement A general enhancement and removed type: bug A general bug labels Jan 27, 2025
@mp911de mp911de changed the title Why default implementation JpaRepository doesn't support JpaSort.unsafe(...)? Translate JpaSort.unsafe(…) used with Specifications to order-by Expression Jan 27, 2025
@mp911de mp911de marked this as a duplicate of #1946 Jan 27, 2025
@mp911de mp911de assigned mp911de and unassigned gregturn Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants