I would like to have a method with a query that I specified for a repository instead of hibernate creating one with methods, but I get these errors.
Model class
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Pokemons {
@Id
private UUID id;
private String name;
private String levels;
}
repository
@Repository
public interface pokemonrepository extends CrudRepository<Pokemons, UUID> {
List<Pokemons> findAllByName(String name);
@Query("SELECT p.name FROM Pokemons p")
List<String> getAllNames();
}
application.properties
spring.sql.init.platform=h2
logging.level.org.hibernate.SQL=DEBUG
spring.jpa.show-sql=true
Error that I get :- (something to do with SpelExtractor?)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pokemonrepository' defined in com.arakooai.test.arakooaiTest.repositories.pokemonrepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: 'int org.springframework.data.repository.query.SpelQueryContext$SpelExtractor.size()'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:967) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:942) ~[spring-context-6.0.11.jar:6.0.11]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.11.jar:6.0.11]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.2.jar:3.1.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.2.jar:3.1.2]
at com.arakooai.test.arakooaiTest.ArakooaiTestApplication.main(ArakooaiTestApplication.java:10) ~[classes/:na]
Caused by: java.lang.NoSuchMethodError: 'int org.springframework.data.repository.query.SpelQueryContext$SpelExtractor.size()'
at org.springframework.data.jpa.repository.query.StringQuery$ParameterBindingParser.parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(StringQuery.java:240) ~[spring-data-jpa-3.1.3.jar:3.1.3]
at org.springframework.data.jpa.repository.query.StringQuery.<init>(StringQuery.java:85) ~[spring-data-jpa-3.1.3.jar:3.1.3]
at org.springframework.data.jpa.repository.query.DeclaredQuery.of(DeclaredQuery.java:40) ~[spring-data-jpa-3.1.3.jar:3.1.3]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.assertParameterNamesInAnnotatedQuery(JpaQueryMethod.java:165) ~[spring-data-jpa-3.1.3.jar:3.1.3]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:146) ~[spring-data-jpa-3.1.3.jar:3.1.3]
at org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory.build(DefaultJpaQueryMethodFactory.java:44) ~[spring-data-jpa-3.1.3.jar:3.1.3]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94) ~[spring-data-jpa-3.1.3.jar:3.1.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:111) ~[spring-data-commons-3.1.2.jar:3.1.2]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:99) ~[spring-data-commons-3.1.2.jar:3.1.2]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:88) ~[spring-data-commons-3.1.2.jar:3.1.2]
at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:88) ~[spring-data-commons-3.1.2.jar:3.1.2]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:357) ~[spring-data-commons-3.1.2.jar:3.1.2]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279) ~[spring-data-commons-3.1.2.jar:3.1.2]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:245) ~[spring-data-commons-3.1.2.jar:3.1.2]
at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-3.1.2.jar:3.1.2]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285) ~[spring-data-commons-3.1.2.jar:3.1.2]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132) ~[spring-data-jpa-3.1.3.jar:3.1.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1817) ~[spring-beans-6.0.11.jar:6.0.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.11.jar:6.0.11]
... 16 common frames omitted
Writing queries with method names work. Defining a custom one doesn't.
I have tried writing a named query for the pokemons model class but then too I got the above spel extractor error. I have no idea how to resolve it.
It is because your spring-data-commons
version does not match with the spring-data-jpa
version.
You are using spring-data-jpa-3.1.3
which will expect the SpelExtractor
from spring-data-commons
will have the size()
method which is added in the 3.1.3. But now you are using spring-data-commons-3.1.2
and so it cannot find this method and throw NoSuchMethodError
.
As you are using spring boot 3.1.2 , it ie better to align both the spring-data-jpa
and spring-data-commons
to be in the same 3.1.2 version.