Hello guys, if you are struggling with "No property save found for type class" In while working with Spring Data JPA then you have come to the right place.
Earlier, I have shared the
best Spring Data JPA courses
and in this tutorial, we are going to discuss how to fix Spring Data JPA - "No
property found for type" exception. This is a common type of error that we can
find on Spring applications because of many reasons. We are going to discuss
some of the occasions where this error happens and it may help to solve the
error. So we will go with examples of what kind of problems that the developer's
might get related to the "No property found for type" exception in Spring Data JPA.
How to fix Spring Data JPA - "No Property Found for Type class - Error 1
One of the error types the users might get related to the customs data
repository is given below with the solution to it.
This is the interface that is used.
public interface FilterRepositoryCustom {
List<User> filterBy(String role);
}
The implementation is like below.
public class FilterRepositoryImpl implements FilterRepositoryCustom {
// some code
}
And the main repository, extending the custom repository as below.
public interface UserRepository extends JpaRepository<User, String>, FilterRepositoryCustom {
// some code
}After running this program, there getting an error calling the following.
org.springframework.data.mapping.PropertyReferenceException: No property filterBy found for type User!
According to the above question, the problem in here is that your are creating FilterRepositoryImpl, but you are using it in UserRepository. You need to create UserRepositoryImpl to make this work.
So you need to make the code as below to solve this issue.
public interface UserRepositoryCustom {
List<User> filterBy(String role);
}
public class UserRepositoryImpl implements UserRepositoryCustom {
// your code
}
public interface UserRepository extends JpaRepository<User, String>, UserRepositoryCustom {
// your actual code
}This is must that the customMethod() in the CustomRepository can only have parameters defined that are either.
1. Entity class name - customMethod(User user)
2. Entity class attributes - customMethod(String firstName), here firstName is an attribute of User Entity class.
You can not have the customMethod(CustomCriteria criteria), the criteria class contain the various attributes that are used to construct a dynamic delay.
example -getStatusByCriteria(CustomCriteria criteria) , CustomCriteria is a simple pojo annotated with @Component so that spring identifies
"No Property Found for Type class - Error 2
In here, there is an application to upload files by the user and this is need to save the bytes of files which is uploaded and wants to create custom repository for that. The code used in there are given below.
public interface MediaBytesRepository
{
public byte[] getBytes(Media media) throws IOException;
public void saveBytes(Media media, byte[] bytes) throws IOException;
public void appendBytes(Media media, byte[] bytes) throws IOException;
public void deleteBytes(Media media) throws IOException;
public boolean bytesExist(Media media) throws IOException;
}Then provided an implementation for this interface called MediaBytesRepositoryIml. With this created the following interface.
public interface MediaRepository extends JpaRepository<Media, Long>, MediaBytesRepository{// code
}After starting the server, the following error appear on the console.
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mediaRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract void com.foo.bar.core. media.MediaBytesRepository.saveBytes(com.foo.bar.core.media.Media,byte[]) throws java.io.IOException!
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport .doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
.....
Caused by: java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract void com.foo.bar.core.media.MediaBytesRepository .saveBytes(com.foo.bar.core.media.Media,byte[]) throws java.io.IOException!
at org.springframework.data.jpa.repository.query. JpaQueryLookupStrategy$CreateQueryLookupStrategy .resolveQuery(JpaQueryLookupStrategy.java:92)
at org.springframework.data.jpa.repository.query .JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy .resolveQuery(JpaQueryLookupStrategy.java:162)
at org.springframework.data.jpa.repository.query .JpaQueryLookupStrategy$AbstractQueryLookupStrategy .resolveQuery(JpaQueryLookupStrategy.java:68)
at org.springframework.data.repository.core.support .RepositoryFactorySupport$QueryExecutorMethodInterceptor .<init>(RepositoryFactorySupport.java:280)
at org.springframework.data.repository.core.support .RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148)
at org.springframework.data.repository.core.support .RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
at org.springframework.data.repository.core.support .RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport .doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 20 more
Caused by: java.lang.IllegalArgumentException: No property save found for type class com.foo.bar.core.media.Media
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:73)
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:92)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:319)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:333)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:301)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:265)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:239)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:70)
at org.springframework.data.repository.query.parser .PartTree$OrPart.<init>(PartTree.java:180)
at org.springframework.data.repository.query.parser .PartTree$Predicate.buildTree(PartTree.java:260)
at org.springframework.data.repository.query.parser .PartTree$Predicate.<init>(PartTree.java:240)
at org.springframework.data.repository.query.parser .PartTree.<init>(PartTree.java:68)
at org.springframework.data.jpa.repository.query. PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)So how can we fix this error? Given below is how to solve this error.
Here also appear to have the naming convention error. You need to rename the MediaBytesRepository to MediaRepositoryCustom and of cause you need an implementation of MediaRepositoryCustom with the name MediaRepositoryImpl.
Let's have an another error which might you get in spring data jpa.
Trying to implement a custom Spring repository with the following interface.
public interface FilterRepositoryCustom {
List<User> filterBy(String role);}
the implementation.
public class FilterRepositoryImpl implements FilterRepositoryCustom {
...}
and the "main" repository, extending my custom repository:
public interface UserRepository extends JpaRepository<User, String>, FilterRepositoryCustom {
...
}When I run my application, I get this error.
org.springframework.data.mapping.PropertyReferenceException: No property filterBy found for type User!
The problem here is that you are creating FilterRepositoryImpl but you are using it in UserRepository. You need to create UserRepositoryImpl to make this work.
Basically,
public interface UserRepositoryCustom {
List<User> filterBy(String role);
}
public class UserRepositoryImpl implements UserRepositoryCustom {
...
}
public interface UserRepository extends JpaRepository<User, String>, UserRepositoryCustom {
...
}Spring Data 2.x updateIf you are facing issue on Spring Data 2.x then you should remember that the naming expectations changed with Spring Data 2.0. The implementation changed from the-final-repository-interface-name-with-an-additional-Impl-suffix to the-custom-interface-name-with-an-additional-Impl-suffix.
Other Java and Spring Tutorial you may like
Thanks for reading this article so far. If you find this
Spring Data JPA Tutorial and Example, then please share it with your
friends and colleagues. If you have any questions or feedback, then please
drop a note.
P. S. - If you are a beginner and want to learn the Framework from scratch and look for some of the best online resources, you can also check out these best Spring MVC courses for beginners. This list contains free Udemy and Pluralsight courses to learn Spring MVC from scratch.
- How to update an entity using Spring Data JPA? (example)
- What is @Conditional annotation in Spring? (conditional example)
- How Spring MVC works internally? (answer)
- Spring Data JPA @Query Example (query example)
- Spring Data JPA Repository (JpaReposistory example)
- 20+ Spring MVC Interview Questions for Programmers (answer)
- 13 Spring Boot Actuator Interview questions (boot questions)
- Difference between @Autowired and @Inject in Spring? (answer)
- Top 5 Frameworks Java Developer Should Know (frameworks)
- 10 Advanced Spring Boot Courses for Java developers (courses)
- Difference between @RequestParam and @PathVariable in Spring (answer)
- Top 7 Courses to learn Microservices in Java (courses)
- How to use @Bean in Spring framework (Example)
- 5 Spring Cloud annotations Java programmer should learn (cloud)
- Top 5 Courses to Learn and Master Spring Cloud (courses)
- 5 Courses to Learn Spring Security for Java programmers (courses)
- Top 5 Spring Boot Annotations Java Developers should know (read)
- 10 Spring MVC annotations Java developer should know (annotations)
- @SpringBootApplication vs. @EnableAutoConfiguration? (answer)
- 15 Spring Boot Interview Questions for Java Developers (questions)
- Difference between @Component, @Service, and @Controller in Spring (answer)
P. S. - If you are a beginner and want to learn the Framework from scratch and look for some of the best online resources, you can also check out these best Spring MVC courses for beginners. This list contains free Udemy and Pluralsight courses to learn Spring MVC from scratch.
No comments :
Post a Comment