[Solved] Spring Data JPA :QueryCreationException: Could not create query for public abstract method XYZ


In Spring boot Data JPA this exception QueryCreationException occurred when there is auto creation of Query or mismatch or column name in column/property in Entity class.

Spring Data JPA supports two ways of creating auto generation of query:

  • JQL in Spring data JPA or HQL when using the Hibernate.
  • Spring Data methods in Repository interface for Example : findByUserName() or findByUserNameOrDesignation() etc.

Reason for Exception

Spring Data JPA generate queries based on name of property in your Entity class , If entity based custom methods defined in repository interfaces also generate queries internally based on method name.

If there is any mismatch or incorrect name in custom methods of repository interface when comparing to entity class properties then on compile time it will not through any issue but when you run or deploy spring boot application will through QueryCreationException: Could not create query for method XYZ.

For Example : In User Entity class there is property userName while in custom methods in repository, you are trying to get detail from method findByName() so this property name not exist in User Entity class . Till compile time this method will not show any issue and compile successfully while on deploy time because using Spring Data JPA so will try to fetch data by Name and it will through exception as QueryCreationException: Could not create query for method XYZ.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userrepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.FacingIssuesOnIT.repository.UserRepository 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.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! Reason: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.9.jar:5.3.9]
	Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.FacingIssuesOnIT.repository.UserRepository 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.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! Reason: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
		... 25 common frames omitted
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! Reason: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!
	at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.5.3.jar:2.5.3]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:106) ~[spring-data-commons-2.5.3.jar:2.5.3]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.5.3.jar:2.5.3]
			... 35 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.FacingIssuesOnIT.repository.UserRepository.findByUsername(java.lang.String)! No property username found for type User!
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.5.3.jar:2.5.3]
	at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:107) ~[spring-data-jpa-2.5.3.jar:2.5.3]
		... 57 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property username found for type User!
	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.5.3.jar:2.5.3]
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.5.3.jar:2.5.3]
	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.5.3.jar:2.5.3]
		... 61 common frames omitted

Solutions

To solve “QueryCreationException” in Spring boot data JPA follow these steps:

  • Go to stack trace of exception and see which query and methods of repository interface is throwing the exception.
  • Go to repository interface where this particular query and methods are defined and also check the Entity name with respect to repository.
  • Go to Entity class and check the properties name defined in method/queries are same or not.
  • If it’s not same or some typing mistake then only you will get this exception.

Note: Whenever you change any property name / column name in Entity class then always check for dependency in Native Queries/ JQL/HQL and Custom methods in repository otherwise your application will not deploy and fail on runtime.

Let me know your thoughts on it.

Happy Learning !!!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s