8
votes

i use querydsl that's why i don't need method like findByName() and all my repository interface are empty.

So i try to make genric code to avoid repetitive interface with empty methods because i have many classes in my entities mapped by hibernate.

public interface GenericResposotory<T> 
              extends JpaRepository<T, Integer>, QueryDslPredicateExecutor<T> {

}

When I run my server I get this error :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericResposotory': 
Invocation of init method failed; nested exception is 
java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object

also there is not a way to make a generic repository like i try to do ?

2
which spring version are you on? - Bond - Java Bond
i use 4.0.5.RELEASE for spring and 1.6.0.RELEASE for spring-data-jpa - Youssef

2 Answers

2
votes

Spring data tries to create beans for all interfaces you create that extend JpaRepository. If you want to have a kind of base repository that will not be used mark your interface with @NoRepositoryBean

0
votes

Repository is a marker interface for spring, that helps to find your own extended repositories and create repository by extracting the type of the entity. We can see in RepositoryFactorySupport:

Factory bean to create instances of a given repository interface. Creates a proxy implementing the configured * repository interface and apply an advice handing the control to the
QueryExecuterMethodInterceptor . Query * detection strategy can be configured by setting QueryLookupStrategy.Key.

That's why, i think, you can't create generic repository directly.

But you can try to use RepositoryFactorySupport (implemented JpaRepositoryFactory) to generate repositories and put it into context manually.

Also, you can reduce the number of files in project and namespace pollution by defining repositories as inner interfaces: just add an attribute as shown below:

<jpa:repositories base-package="com.pack" consider-nested-repositories="true"/>