今天在使用springboot对MySQL进行增删改查操作时突然报错:
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-07-28T16:30:38.337+08:00 ERROR 29612 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'opportunityMatching': Unsatisfied dependency expressed through field 'iUserRepository': Error creating bean with name 'IUserRepository' defined in app.test4.OpportunityMatching.IUserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract app.test4.OpportunityMatching.User app.test4.OpportunityMatching.IUserRepository.findByEmail(java.lang.String); Reason: Failed to create query for method public abstract app.test4.OpportunityMatching.User app.test4.OpportunityMatching.IUserRepository.findByEmail(java.lang.String); Unable to locate Attribute with the given name [email] on this ManagedType [app.test4.OpportunityMatching.User]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:714) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:694) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:483) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.10.jar:6.0.10]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.10.jar:6.0.10]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.1.jar:3.1.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.1.jar:3.1.1]
at app.test4.Test4Application.main(Test4Application.java:20) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IUserRepository' defined in app.test4.OpportunityMatching.IUserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract app.test4.OpportunityMatching.User app.test4.OpportunityMatching.IUserRepository.findByEmail(java.lang.String); Reason: Failed to create query for method public abstract app.test4.OpportunityMatching.User app.test4.OpportunityMatching.IUserRepository.findByEmail(java.lang.String); Unable to locate Attribute with the given name [email] on this ManagedType [app.test4.OpportunityMatching.User]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:711) ~[spring-beans-6.0.10.jar:6.0.10]
... 20 common frames omitted
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract app.test4.OpportunityMatching.User app.test4.OpportunityMatching.IUserRepository.findByEmail(java.lang.String); Reason: Failed to create query for method public abstract app.test4.OpportunityMatching.User app.test4.OpportunityMatching.IUserRepository.findByEmail(java.lang.String); Unable to locate Attribute with the given name [email] on this ManagedType [app.test4.OpportunityMatching.User]
at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:115) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:99) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:88) ~[spring-data-commons-3.1.1.jar:3.1.1]
at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.(QueryExecutorMethodInterceptor.java:88) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:357) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:245) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.10.jar:6.0.10]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.10.jar:6.0.10]
... 30 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract app.test4.OpportunityMatching.User app.test4.OpportunityMatching.IUserRepository.findByEmail(java.lang.String); Unable to locate Attribute with the given name [email] on this ManagedType [app.test4.OpportunityMatching.User]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:107) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:124) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:258) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:111) ~[spring-data-commons-3.1.1.jar:3.1.1]
... 42 common frames omitted
Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute with the given name [email] on this ManagedType [app.test4.OpportunityMatching.User]
at org.hibernate.metamodel.model.domain.AbstractManagedType.checkNotNull(AbstractManagedType.java:287) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
at org.hibernate.metamodel.model.domain.AbstractManagedType.getAttribute(AbstractManagedType.java:160) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
at org.hibernate.metamodel.model.domain.AbstractManagedType.getAttribute(AbstractManagedType.java:51) ~[hibernate-core-6.2.5.Final.jar:6.2.5.Final]
at org.springframework.data.jpa.repository.query.QueryUtils.requiresOuterJoin(QueryUtils.java:835) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:776) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:755) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:751) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:387) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:310) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:210) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:121) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:60) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:119) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:95) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:81) ~[spring-data-commons-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.(PartTreeJpaQuery.java:218) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.(PartTreeJpaQuery.java:361) ~[spring-data-jpa-3.1.1.jar:3.1.1]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:102) ~[spring-data-jpa-3.1.1.jar:3.1.1]
... 46 common frames omitted
日志内容很多,但是主要大概就是:
Email的名字有问题,不能使用email而是Email
我把代码检查了一圈,都没有使用email的地方,然后就觉得很奇怪
务必与MySQL上面的完全一致
多半是自动生成查询语句的时候自动生成的与数据库的不相同(重点发生在数据库中为大写的时候)
因为一般情况下生成的都是小写:
所以最好是将MySQL中的大写的字段换成小写的字段(一般数据库的字段都是使用小写命名的,数据表也是,不会用大写命名,容易报错)
如果不想换,就必须自定义查询语句的实现,不要让他自动生成的语句.
其实不是大小写
本文由 mdnice 多平台发布