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 ?

Hayi
  • 6,972
  • 26
  • 80
  • 139

2 Answers2

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

Nadir
  • 1,369
  • 1
  • 15
  • 28
  • I mark it but i get `Could not autowire field: private repository.GenericRespository` when i try `@Autowired private GenericRespository region_repository;` – Hayi Nov 21 '14 at 10:12
  • 1
    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. – Nadir Nov 21 '14 at 10:30
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"/>
retroq
  • 572
  • 2
  • 7