2

I have created an interface like this,

@Repository
public interface IJpaRepositoryCustom<T> extends JpaRepository<T,Long>{
}

And service class

@Service
public class LOVService<T>{
    @Autowired
    private IJpaRepositoryCustom<T> jpaRepositoryCustom;
}

But with the above code, I am getting an exception

SEVERE [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.core.StandardContext.listenerStart 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 'IJpaRepositoryCustom': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)

Is there any other way to create a common JPA repository?

Thanks in advance.

Pawan Tiwari
  • 518
  • 6
  • 26
  • 3
    Possible duplicate of [Using generics in Spring Data JPA repositories](https://stackoverflow.com/questions/19417670/using-generics-in-spring-data-jpa-repositories) – Jens Schauder Oct 15 '18 at 07:24

2 Answers2

2

When we want to add custom methods into all Spring Data JPA repositories, the first thing that we have to is to create a base interface which declares the custom methods.

1) First Step:Create Base Interface

    //Annotation to exclude repository interfaces from being picked up and thus in consequence getting an instance being created.
@NoRepositoryBean  
public interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> {

    T findOne(ID id);

    List<T> findAll();

}

2) Second Step : Implementing the Base Repository Interface

public interface IUserReadOnlyRepository extends ReadOnlyRepository<User,Integer> 

{
    User findById(Integer id);

    List<User> findByName(String name);

    @Query("select u from MyUser u where u.name = ?1")
    List<User> findUserByAttributeAndValueCustomQuery(String string2);   
}

3) Third Step: Just Autowired into Controller/RESTController

  @RestController
    @RequestMapping("/user")
    @CrossOrigin
    //@EnableTransactionManagement
    public class UserController {

        @Autowired
        private IUserReadOnlyRepository userReadOnlyRepository;

    @RequestMapping(value = "/getUserByIdReadOnlyRepository/{id}")
    public @ResponseBody User getUserById(@PathVariable Integer id)
    { 
        User user = userReadOnlyRepository.findById(id);
        return user;

    }

Note: It is perfectly working for me!. If any help required, ping me.

Angad Bansode
  • 825
  • 6
  • 15
  • **Thanks for the reply** yes this is perfect example for add custom methods into all Spring Data JPA repositories, but my question is different. In your example you have to create multiple repository interfaces for each entity. But I want to create common repository for all the entities. – Kalana Weerarathne Oct 15 '18 at 08:36
  • @KalanaWeerarathne Its not a good idea to have a common repo for all the entities. Pls think about SRP. – Kevin Dec 08 '20 at 09:21
0

You can edit your service like this :

@Service
public class LOVService{

    @Autowired
    private IJpaRepositoryCustom jpaRepositoryCustom;
}

And your repo:

@Repository
public interface IJpaRepositoryCustom extends JpaRepository<YourEntityClass,Long>{
}

And there is no need to pass the argument to the repository if you're autowiring this.

I hope this will help.

Pawan Tiwari
  • 518
  • 6
  • 26