我有一个应用程序,计算Roto值从给定的统计。我重构了它,使用@MapperSuperclass,而不是只保存1个实体(额外的字段设置为0),现在我得到了一个错误。奇怪的是,我的应用程序仍然运行并完成其工作,只有在应该关闭时才会抛出错误。
这是我的结束语。除了遇到Exception的语句之外,这部分与重构之前完全相同。我不明白的是为什么它试图刷新。
2023-03-27 11:57:01.940 INFO 6588 --- [ main] com.baseball.roto.service.ExcelService : writing results
2023-03-27 11:57:02.102 INFO 6588 --- [ main] com.baseball.roto.service.ExcelService : wrote stats
2023-03-27 11:57:02.135 INFO 6588 --- [ main] com.baseball.roto.Runner : done
2023-03-27 11:57:02.206 WARN 6588 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'statsRepository' defined in com.baseball.roto.repository.StatsRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Not an entity: class com.baseball.roto.model.entity.Stats; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Not an entity: class com.baseball.roto.model.entity.Stats
2023-03-27 11:57:02.208 INFO 6588 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2023-03-27 11:57:02.213 INFO 6588 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2023-03-27 11:57:02.249 INFO 6588 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
它不再像以前那样在前面的语句后关闭,而是继续-
2023-03-27 11:57:02.259 INFO 6588 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-03-27 11:57:02.288 ERROR 6588 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'statsRepository' defined in com.baseball.roto.repository.StatsRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Not an entity: class com.baseball.roto.model.entity.Stats; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Not an entity: class com.baseball.roto.model.entity.Stats
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:934) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.20.jar:5.3.20]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.20.jar:5.3.20]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.0.jar:2.7.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.0.jar:2.7.0]
at com.baseball.roto.Roto2Application.main(Roto2Application.java:10) ~[classes/:na]
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Not an entity: class com.baseball.roto.model.entity.Stats; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Not an entity: class com.baseball.roto.model.entity.Stats
at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:106) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.7.0.jar:2.7.0]
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]
at java.base/java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1054) ~[na:na]
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:96) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.7.0.jar:2.7.0]
at java.base/java.util.Optional.map(Optional.java:265) ~[na:na]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:364) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:322) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:328) ~[spring-data-commons-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.20.jar:5.3.20]
... 15 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.baseball.roto.repository.StatsRepository.findAllByName(java.lang.String)! Not an entity: class com.baseball.roto.model.entity.Stats
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:113) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:254) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:87) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102) ~[spring-data-commons-2.7.0.jar:2.7.0]
... 37 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not an entity: class com.baseball.roto.model.entity.Stats
at org.hibernate.metamodel.internal.MetamodelImpl.entity(MetamodelImpl.java:566) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
at org.hibernate.query.criteria.internal.QueryStructure.from(QueryStructure.java:128) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
at org.hibernate.query.criteria.internal.CriteriaQueryImpl.from(CriteriaQueryImpl.java:158) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.<init>(JpaQueryCreator.java:88) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.query.JpaCountQueryCreator.<init>(JpaCountQueryCreator.java:48) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.createCreator(PartTreeJpaQuery.java:365) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:211) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:348) ~[spring-data-jpa-2.7.0.jar:2.7.0]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:91) ~[spring-data-jpa-2.7.0.jar:2.7.0]
... 41 common frames omitted
所以很明显,这与添加@MappedSuperclass有关,但我不知道是什么。
这是我的repo-
public interface StatsRepository<T extends Stats> extends CrudRepository<T, StatsId> {
List<T> findAllByWeek(int week);
List<T> findAllByName(String name);
}
这两个子节点都只是用它们的Pojo扩展这个接口
public interface ChampRepository extends StatsRepository<ChampStats> { }
我的配置文件提供了我的服务的Repo bean-
@Configuration
public class RepositoryConfiguration {
@Autowired private StatsRepository<ChampStats> champRepository;
@Autowired private StatsRepository<PsdStats> psdRepository;
@Bean
public StatsRepository repository(League league) {
if (league.equals(League.CHAMPIONS)) {
return champRepository;
} else if (league.equals(League.PSD)) {
return psdRepository;
}
throw new BadInput("The given league is not valid");
}
}
1条答案
按热度按时间2q5ifsrm1#
我仍然不明白为什么这个错误只发生在应用运行完成之后,但错误的原因是Sping Boot 试图将StatsRepository接口初始化为Spring bean,因为它扩展了CrudRepository。
修复方法只是将
@NoRepositoryBean
注解添加到StatsRepository