Jpa自定義查詢報錯
問題背景
今天遇到一個奇怪的報錯“Failed to convert from type [java.lang.Object[]] to type”,這個報錯,百度上也是很少的,恰恰是這樣的問題,引起我了解決的欲望。先看看報錯:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [org.microservice.tcbj.yytsg.checksys.entity.Wicket] for value '{1, 7, C1, 1, 7, 1}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [org.microservice.tcbj.yytsg.checksys.entity.Wicket]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:46)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:108)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvo
在網上找了很多解決方案,多是要改變Springboot的配置,然而給便配置我肯定是不願意的,於是經過千辛萬苦,終於找到了解決方案。
出現該錯誤的原因可能是你在當前的Repository里面,查詢了其他的實體,例如你在UserRepository extends JpaRepository<UserEntity,Integer>里面去自定義了一個查詢,該查詢返回的實體是CarEntity,那就會報這樣的轉換錯誤,每個實體要都要有一個對應的XXXRepository來執行CRUD操作的。實體Entity跟Repository層內容要對應,解決方案就是將這個自定義查詢剪切到對應的Repository里面。
