8

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? Commented Nov 21, 2014 at 7:01
  • i use 4.0.5.RELEASE for spring and 1.6.0.RELEASE for spring-data-jpa Commented Nov 21, 2014 at 7:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I mark it but i get Could not autowire field: private repository.GenericRespository when i try @Autowired private GenericRespository<Region> region_repository;
Ok, so I misunderstood the question. I thought that this interface is a base for other things and you extend it with other interfaces. In that case I dont think it's possible to achieve what you want. You simply have to extend the genericinterface with some other and set T to some class.
0

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"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.