使用 Spring Data 进行复杂查询(使用具有 ManyToOne 关系的 2 个表)
Posted
技术标签:
【中文标题】使用 Spring Data 进行复杂查询(使用具有 ManyToOne 关系的 2 个表)【英文标题】:Complex query with Spring Data (using 2 tables with relation ManyToOne) 【发布时间】:2020-06-25 15:22:14 【问题描述】:我正在研究弹簧靴。所以,我有两个实体(“Projet”和“CQP”),我想通过 CQP 的名字查找项目。
PS:我使用了关系 ManyToOne,它在“Projet”表中生成一个名为 cqp_id 的列
所以我在 projetRepository 中创建了这个查询:
@Query("SELECT p FROM Projet p WHERE p.CQP.firstname IN (SELECT id FROM CQP WHERE CQP.firstname like ?1)")
List<Projet> findByFirstname(String firstname);
但是当我运行控制台中出现错误的应用程序时
2020-03-13 16:31:07.277 错误 5404 --- [restartedMain] os.boot.SpringApplication:应用程序运行失败 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“pfeProjectApplication”的bean时出错:通过字段“projetservice”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“projetImpl”的 bean 时出错:通过字段“projetrepository”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“projetRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: Validation failed for query for public abstract java.util.List com.example.demo.dao.ProjetRepository.findByFirstname(java.lang.String)! 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 com.example.demo.PfeProjectApplication.main(PfeProjectApplication.java:32) [classes/:na] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101] 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)~[na:1.8.0_101] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)~[na:1.8.0_101] 在 java.lang.reflect.Method.invoke(未知来源)~[na:1.8.0_101] 在 org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.4.RELEASE.jar:2.2.4.RELEASE] 原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“projetImpl”的bean时出错:通过字段“projetrepository”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“projetRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: Validation failed for query for public abstract java.util.List com.example.demo.dao.ProjetRepository.findByFirstname(java.lang.String)! 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] ...省略了24个常用框架 原因:org.springframework.beans.factory.BeanCreationException:创建名为“projetRepository”的bean时出错:调用init方法失败;嵌套异常是 java.lang.IllegalArgumentException: Validation failed for query for public abstract java.util.List com.example.demo.dao.ProjetRepository.findByFirstname(java.lang.String)! 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] ...省略了37个常用框架 原因:java.lang.IllegalArgumentException:查询方法 public abstract java.util.List com.example.demo.dao.ProjetRepository.findByFirstname(java.lang.String) 的验证失败! 在 org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.jpa.repository.query.SimpleJpaQuery.(SimpleJpaQuery.java:63) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:76) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:56) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:140) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:207) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:78) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:574) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:567) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 java.util.stream.ReferencePipeline$3$1.accept(Unknown Source) ~[na:1.8.0_101] 在 java.util.Iterator.forEachRemaining(Unknown Source) ~[na:1.8.0_101] 在 java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Unknown Source) ~[na:1.8.0_101] 在 java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source) ~[na:1.8.0_101] 在 java.util.stream.AbstractPipeline.copyInto(未知来源)~[na:1.8.0_101] 在 java.util.stream.AbstractPipeline.wrapAndCopyInto(未知来源)~[na:1.8.0_101] 在 java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source) ~[na:1.8.0_101] 在 java.util.stream.AbstractPipeline.evaluate(未知来源)~[na:1.8.0_101] 在 java.util.stream.ReferencePipeline.collect(未知来源)~[na:1.8.0_101] 在 org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.mapMethodsToQuery(RepositoryFactorySupport.java:569) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$new$0(RepositoryFactorySupport.java:559) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 java.util.Optional.map(未知来源)~[na:1.8.0_101] 在 org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:559) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:332) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.util.Lazy.getNullable(Lazy.java:212) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300) ~[spring-data-commons-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:121) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE] ...省略了47个常用框架 引起:java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:cqp_id of:com.example.demo.entities.Projet [SELECT p FROM com.example.demo.entities.Projet p WHERE p.cqp_id IN (SELECT id FROM com.example.demo.entities.CQP WHERE CQP.firstname like ?1)] 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:718) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101] 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)~[na:1.8.0_101] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)~[na:1.8.0_101] 在 java.lang.reflect.Method.invoke(未知来源)~[na:1.8.0_101] 在 org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:368) ~[spring-orm-5.2.3.RELEASE.jar:5.2.3.RELEASE] 在 com.sun.proxy.$Proxy86.createQuery(Unknown Source) ~[na:na] 在 org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87) ~[spring-data-jpa-2.2.4.RELEASE.jar:2.2.4.RELEASE] ...省略了76个常用框架 引起:org.hibernate.QueryException:无法解析属性:cqp_id of:com.example.demo.entities.Projet [SELECT p FROM com.example.demo.entities.Projet p WHERE p.cqp_id IN(SELECT id FROM com .example.demo.entities.CQP WHERE CQP.firstname like ?1)] 在 org.hibernate.QueryException.generateQueryException(QueryException.java:120) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:220) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:113) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:73) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:155) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:600) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:709) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] ...省略84个常用框架 引起:org.hibernate.QueryException:无法解析属性:cqp_id of:com.example.demo.entities.Projet 在 org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:73) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:67) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:2033) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:407) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:516) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:697) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:275) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:215) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:1045) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1294) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4715) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4497) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2147) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:819) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:613) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:317) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:265) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:272) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] ...省略90个常用框架【问题讨论】:
【参考方案1】:不需要子查询,但无论如何您的子查询都是错误的,因为您将firstname
与CQP.id
进行比较。
你可以这样做:
import org.springframework.data.repository.query.Param;
...
@Query("SELECT p FROM Projet p WHERE p.CQP.firstname LIKE %:firstname%")
List<Projet> findByFirstname(@Param("firstname") String firstname);
或者,如果您需要不区分大小写的比较,只需将UPPER()
添加到两者:
import org.springframework.data.repository.query.Param;
...
@Query("SELECT p FROM Projet p WHERE UPPER(p.CQP.firstname) LIKE %UPPER(:firstname)%")
List<Projet> findByFirstname(@Param("firstname") String firstname);
如果您将比较保留为 p.CQP.firstname LIKE :firstname
,则与进行严格的相等检查 (p.CQP.firstname = :firstname
) 相同,我认为这不是您所追求的。
【讨论】:
感谢您的回答,但是当我运行项目时同样的问题我将“nativequery = true”添加到查询中@Query(value="SELECT p FROM Projet p WHERE p.CQP. firstname LIKE %:firstname%", nativeQuery = true) 所以所有错误都消失了,但是当我在邮递员中测试时,正文是空的 嗯,您的实体中可能存在一些错误配置。你能告诉他们吗? (只有有趣的部分,它们之间的关系定义) 在我创建的“Projet”实体中:@ManyToOne private CQP cqp;它会在数据库中自动生成一个名为 cqp_id 的列 但在 CQP 实体中我只声明了它们的属性(id、firstname 和 lastname) 你能告诉我是否有什么要添加到实体中的吗?以上是关于使用 Spring Data 进行复杂查询(使用具有 ManyToOne 关系的 2 个表)的主要内容,如果未能解决你的问题,请参考以下文章
使用具有一个条件的 WHERE 子句运行 100 个 SQL 查询,还是使用具有 100 个条件的 WHERE 子句的一个查询更好?
spring data jpa Specification 复杂查询+分页查询