Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot JPA CrudRepository

I'm working with Spring Boot + Spring Data JPA and facing this problem when trying to inject a class that extends CrudRepository:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'topicRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

Repository Class:

public interface TopicRepository extends CrudRepository<Topic, Integer> {}

Service Class:

@Service
public class TopicService {

      @Autowired
      private TopicRepository topicRepository;
}

Any suggestions?

like image 846
A.Chakroun Avatar asked Feb 15 '17 15:02

A.Chakroun


People also ask

Is CrudRepository a JPA?

No. Crud Repository is the base interface and it acts as a marker interface. JPA also provides some extra methods related to JPA such as delete records in batch and flushing data directly to a database. It provides only CRUD functions like findOne, saves, etc.

What is Spring boot CrudRepository?

CrudRepository is a Spring Data interface for generic CRUD operations on a repository of a specific type. It provides several methods out of the box for interacting with a database.

What is the difference between CrudRepository and JpaRepository in Spring boot?

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 happens if the argument for Save () method of CrudRepository is null?

So if the field is null in the entity to save , it will overwrite its value to null in the existing row.


2 Answers

Which versions of spring-data-commons and spring-data-jpa are you using. I just ran into this using spring-data-commons 1.13.x with spring-data-jpa 1.10.x. Upgrading spring-data-jpa to 1.11.x fixed the issue for me.

like image 115
Andy Sampson Avatar answered Oct 20 '22 12:10

Andy Sampson


I was having the same issue, and I fixed it by switching Spring Boot versions. Changing the Spring Data JPA versions did nothing (this is where I assumed the bug would be), so I think there is a bug in Spring Boot version 1.5.1. I switched back to version 1.4.3 and the error was gone. I didn't try subsequent/different versions, so you may just have to experiment with your dependencies and their versions.

For the record, you can have your service class annotated with @Repository, it shouldn't make any difference. I've been setting these apps up the same way using the service/dao pattern, and it has never been too picky with the annotations. Hopefully this may help others whose Spring Boot development flow suddenly throws an error!

like image 21
Trevor Bye Avatar answered Oct 20 '22 11:10

Trevor Bye