Search code examples
javaspring-data-jpamappedsuperclass

app is attempting to refresh leading to @MappedSuperClass repository errors - Failed to create query for method public abstract, Not an entity


I have an app that calculates Roto values from given Stats. I refactored it to use @MapperSuperclass instead of saving only 1 Entity (with extra fields set to 0) and now I am getting an error. The weird thing is that my app still runs and does its job, and only throws the error when it should be shutting down.

Here are my closing terminal statements. Besides for the statement that an Exception as was encountered, this part is exactly the same as prior to the refactor. What I don't understand is why it is attempting to refresh.

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.

And then instead of shutting down after the prior statement as it used to, it continues on -

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

So obviously this has something to do with adding the @MappedSuperclass but I have no idea what.

Here are my repos -

public interface StatsRepository<T extends Stats> extends CrudRepository<T, StatsId> {
    List<T> findAllByWeek(int week);
    List<T> findAllByName(String name);
}

Both children merely extend this interface with their Pojo

public interface ChampRepository extends StatsRepository<ChampStats> { }

And my config file that gives the Repo bean to my Service -

@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");
    }
} 

Solution

  • I am still confused as to why the error only occurred after the app had finished running but the reason for the error was that Spring Boot creates Spring Beans for any class that extends the CrudRepository even when it has no @Repository annotation.

    The fix was adding the @NoRepositoryBean annotation to the StatsRepository class