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里面。