Spring dynamic JPA repository type

Viewed 5467

I have about 30 tables that I need to fill from an XML file. And I want to use JPA for that purpose.

Now I have 30 classes annotated with @Entity, config that scans entities and repositories;

Also I have:

@Repository
public interface MyRepository extends JpaRepository<MyEntity1, Long> {
}

And (some controller):

@Autowired
public MyRepository myRepository;
...
...
MyEntity1 entity = new MyEntity(...);
myRepository.save(entity);

It works fine with one @Entity but should I define 30 repositories for that?

I thought I could do something like this:

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

and then:

@Autowired
public MyRepository<MyEntity1> myRepository1;
@Autowired
public MyRepository<MyEntity2> myRepository2;

but that gave an error:

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