1

we were facing issues while starting our Spring Boot Application. We added a new API with a Controller, a Service class, and two Repository classes for two tables. This is the master table repository

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;    
import com.sixdee.wallet.core.domain.PromoTicketMaster;

@Repository
public interface PromoTicketMasterRepo extends JpaSpecificationExecutor<PromoTicketMaster> {

}

We had a look at this answer What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA? before creating this Repo as it was used for Pagination methods as well. It said that Because of the inheritance mentioned above, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository . But the application didn't start with this Repo configuration and the console showed this error. The autowiring was fine.

*************************** APPLICATION FAILED TO START
***************************

Description:

Field promoTicketMasterRepo in com.sixdee.wallet.core.service.impl.PromoTicketServiceImpl required a bean of type 'com.sixdee.wallet.core.repository.PromoTicketMasterRepo' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.sixdee.wallet.core.repository.PromoTicketMasterRepo' in your configuration.

Then we changed the Repo like this and the application was started.

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.sixdee.wallet.core.domain.PromoTicketMaster;

@Repository
public interface PromoTicketMasterRepo extends JpaSpecificationExecutor<PromoTicketMaster>, CrudRepository<PromoTicketMaster, String> {

}

What was wrong with the first approach

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
  • Specifications are not a Repository interface. See https://reflectoring.io/spring-data-specifications/ – Chris Mar 23 '22 at 17:41
  • Then @Chris, what is the use of extending the above-mentioned interfaces. – Arun Sudhakaran Mar 24 '22 at 11:11
  • Check out the methods within it and decide for yourself the value; JpaSpecificationExecutor adds search functionality, while CrudRepository and JpaRepository add save/search type methods. Why Spring didn't have one extend the other is a question for Spring, not StackOverFlow, but likely done as away to allow adding the behaviour to any repos - to pick and choose which to apply this to independently. – Chris Mar 24 '22 at 15:06

0 Answers0