I have read quite a bit on type erasure and explicit variance/covariance of generics but I still can't fully understand why this is an unsafe cast:
public interface TestRepository<T extends ParentEntity> extends JpaRepository<T, String> {
List<T> testMethod();
}
public class TestClass() {
private TestRepository testRepository;
public void test() {
List<? extends ParentEntity> entities = testRepository.testMethod(); //not safe
testRepository.saveAll(entities); //safe
}
}
Now I can get this to be safe by using a wildcard on the TestRepository field, but then I get a problem with the saveAll method of Jpa:
public class TestClass() {
private TestRepository<? extends ParentEntity> testRepository;
public void test() {
List<? extends ParentEntity> entities = testRepository.testMethod(); //safe
testRepository.saveAll(entities); //not safe
}
}
What is the best way to make this work without making the TestClass generic?
List– Bgtop Mar 11 '20 at 12:00saveAll(Iterablevar1)` Is there any way to get this working?