I am afraid not. If you already tried you would have noticed that your second repository extending CrudRepository<T, Long>
fails with something like:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object.
because with generic T
java can tell only that it is an Object
which is not an entity. And because of that you need to add annotation @NoRepositoryBean
which then only allows you to extend it with a real entity type instead of generic T
.
It might be possible programmatically but most probably would be a lot more painful than just to create repo interfaces.
If you have entity inheritance tree like A->B->C
you are able to create a repo for A
that then handles also B
& C
what comes to properties inherited from A
. So you could issue findAll()
and it would return you all A
s, B
s & C
s but as a list of A
s and you would have to check the actual type of each item separately.
Usually when there is no need to any special treatment for entities and you do not want to write those repository "stubs" you can just use EntityManager
directly. You can implement your own - generic repository like - @Service
class that has @Autowired
entity manager and invoke its find(..)
, persist(..)
and other methods.