2

I am new to Spring and want to use JpaRepository in a Generic way, so that i can use the same interface for all. Please suggest some examples. Thanks :)

Like i have 2 entities Employee.java & Student.java

@Repository
public interface EmployeeRepo<T, ID extends Serializable> extends JpaRepository<Employee, ID> {

}
SGG
  • 75
  • 1
  • 13
  • You don't need `@Repository`, but you do need an interface per entity type because of generic erasure (and more broadly because Spring doesn't necessarily know which types you would want a repository for). – chrylis -cautiouslyoptimistic- Nov 16 '17 at 03:50
  • Duplicate: https://stackoverflow.com/questions/31753125/generic-crud-in-spring-data-jpa/47322730#47322730 – Jens Schauder Nov 16 '17 at 06:07

1 Answers1

0

There is no way to do this in Spring Data. The inner mechanics of Spring Data need the type parameters from the interface definition and if there is no interface definition with specific type parameters only the bound of the type parameters (Object in case of doubt) is available which is not usable.

Also, you shouldn't have a Repository per Entity but per Aggregate. See this answer for details: Are you supposed to have one repository per table in JPA?

Jens Schauder
  • 65,795
  • 24
  • 148
  • 294