Reputation: 387
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.
Upvotes: 3
Views: 1842
Reputation: 923
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.
Upvotes: 3
Reputation: 537
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.
Upvotes: 0