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

Projections not handled correctly when Optional is used as wrapping return type [DATAJPA-951] #1302

Closed
spring-projects-issues opened this issue Aug 12, 2016 · 7 comments
Assignees
Labels
type: bug

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Aug 12, 2016

Maximos Sapranidis opened DATAJPA-951 and commented

When using Optional return type and JPQL projection to create an object (not an Entity type)
The following exception is thrown:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.Optional<?>] to type [com.cookies.domain.dto.AuthenticationPrincipal]

An exaple repository is:

@Repository
public interface UserPrincipalRepository extends CrudRepository<UserAccountPair, String> {
 
    @Query(value = "SELECT " +
            "new xxx.AuthenticationPrincipal(u.id, u.email, u.password, u.enabled) " +
            "FROM ApiUser u "
            + "WHERE LOWER(u.email) = LOWER(?1)")
    Optional<AuthenticationPrincipal> findUserForAuthentication(String string);
}

In previous versions this was working as expected


Affects: 1.10.2 (Hopper SR2), 1.11 M1 (Ingalls)

Referenced from: commits d75019b, 1f677bd

Backported to: 1.10.3 (Hopper SR3)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Aug 17, 2016

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Aug 17, 2016

Oliver Drotbohm commented

Any chance you add the complete stack trace so that we can see what the calling path is?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Aug 17, 2016

Oliver Trosien commented

I'm facing the same problem. Here's a stack trace.

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.Optional<?>] to type [com.epages.shop.site.FeatureConfiguration]
	at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:176)
	at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:240)
	at org.springframework.data.repository.query.ResultProcessor$ChainingConverter$1.convert(ResultProcessor.java:185)
	at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:196)
	at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:159)
	at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:107)
	at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:92)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
	at com.sun.proxy.$Proxy194.findFeatureMaxValue(Unknown Source)
	at com.epages.shop.site.SiteRefJpaTest.should_get_feature_max_value_by_tenant_id(SiteRefJpaTest.java:56)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Aug 17, 2016

Oliver Trosien commented

Work-around: Create a default method that wraps the original "plain" finder into an Optional.ofNullable()

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Aug 17, 2016

Oliver Drotbohm commented

Looks like Spring Frameworks own Optional converter kicks in too early, when we apply the converter that was actually supposed to handle low level type conversion (e.g. long <-> int, byte arrays etc.). I'll add a guard to explicitly exclude this one

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Aug 17, 2016

Oliver Drotbohm commented

That's fixed in master and the bugfix branch for Hopper. Feel free to give the snapshots a try

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Aug 18, 2016

Yuki YOSHIDA commented

@Query("select new com.example.TestDataSummary(d.id, d.col1) " +
"from TestData d where d.id = ?1")
Optional<TestDataSummary> findOptionalAndNewById(Integer id);

Above query works fine at 1.10.3.BUILD-SNAPSHOT, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug
Projects
None yet
Development

No branches or pull requests

2 participants