Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No property found for type... custom Spring Data repository

I'm trying to implement a custom Spring repository. I have the 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 { ... } 

I'm using Spring Boot and, according to the docs:

By default, Spring Boot will enable JPA repository support and look in the package (and its subpackages) where @SpringBootApplication is located.

When I run my application, I get this error:

org.springframework.data.mapping.PropertyReferenceException: No property filterBy found for type User!

like image 509
Héctor Avatar asked Jan 04 '17 15:01

Héctor


People also ask

What is difference between JpaRepository and CrudRepository?

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

What is @NoRepositoryBean?

The @NoRepositoryBean annotation is another way to fine-tune the spring data JPA repository. @NoRepositoryBean annotation concept is just like Interface in Java . The interface has the declaration of all methods, a base class implements the interface and extends all the methods.

What is EnableJpaRepositories?

Spring Data Jpa provides EnableJpaRepositories annotation which is used to scan the packages for configuration and repository class for Spring Data JPA.

What is JpaSpecificationExecutor?

The JpaSpecificationExecutor<T> interface declares the methods that can be used to invoke database queries that use the JPA Criteria API. This interface has one type parameter T that describes the type of the queried entity.


2 Answers

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.

Read this doc for more detail

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 update
This answer was written for Spring 1.x. As Matt Forsythe pointed out, 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.

So in this case, the name of the implementation would be: UserRepositoryCustomImpl.

like image 71
A0__oN Avatar answered Sep 17 '22 16:09

A0__oN


Another way this error can happen if the impl class for FilterRepositoryCustom isn't picked up in your spring configuration:

@EnableJpaRepositories(basePackageClasses = {RepoPackageMarker.class, FilterRepositoryCustomImpl.class}) 
like image 43
user64141 Avatar answered Sep 17 '22 16:09

user64141