I have my own custom repository but when I try to override/use CrudRepository it gives me "both methods have same erasure, yet neither overrides the other". Please see below.
public interface MyRepository extends CrudRepository<Person, String> {
//<S extends T> S save(S entity); //Parent method
Person save(Person person);//It accepts this
//<S extends T> Iterable<S> saveAll(Iterable<S> entities);//Parent method
Iterable<Person> saveAll(Iterable<Person> persons);//But does not accept this. why can I not use/override this?
//This Gives error "both methods have same erasure, yet neither overrides the other"
}
Java generics work through the concept known as erasure, meaning that under the hood all generics get transformed into <Object>
. This unfortunately is one of the limitations of Java making sure it is backwards compatible with code written before Java 5 and generics.
Compiler sees both methods with identical names and identical parameters.
Just change the name of one of the methods from saveAll
to saveAllPeople
or whatever and it will work.