Passing Iterator to method
java, spring, spring-data
Solution
Can anyone tell me what I'm doing wrong that it's trying to call the " U save(U entity);" method instead of the " Iterable save(Iterable entities);" method?
Well `save(Iterable<U> entities)` clearly isn't valid here, because you have an `Iterator<E>`, not an `Iterable<E>`. They're different types. Given two method overloads which are both invalid for the arguments that you're trying to pass, the compiler has picked one to complain about... it doesn't really matter much which it says is invalid, IMO.
It's not clear why you're calling `iterator()` at all, but you should try just changing your code to:
public void persistLandTiles(List<LandTile> tilesToPersist) {
landTileRepo.save(tilesToPersist);
}
After all, `List<E>` extends `Iterable<E>` - so this method call will be valid...
Problem
I'm trying to pass an iterator as the argument for a method in Spring Data, but eclipse is telling me that Java is trying to call the plain entity overload of the same method. ``` public void persistLandTiles(List<LandTile> tilesToPersist) { landTileRepo.save(tilesToPersist.iterator()); } ``` The error: ``` Bound mismatch: The generic method save(U) of type CRUDRepository<T> is not applicable for the arguments (Iterator<LandTile>). The inferred type Iterator<LandTile> is not a valid substitute for the bounded parameter <U extends LandTile> ``` and the interface methods being called: ``` @NoRepositoryBean public interface CRUDRepository<T> extends PagingAndSortingRepository<T, Long> { /** * persists an entity by forwarding to entity.persist() * @param entity to be persisted * @return the saved entity (being the same reference as the parameter) */ @Transactional <U extends T> U save(U entity); /** * persists the provided entities by forwarding to their entity.persist() methods * @param entities to be persisted * @return the input iterable */ @Transactional <U extends T> Iterable<U> save(Iterable<U> entities); ``` My interface that's being autowired: ``` public interface LandTileRepo extends CRUDRepository<LandTile>, SpatialRepository<LandTile> { } ``` Can anyone tell me what I'm doing wrong that it's trying to call the " U save(U entity);" method instead of the " Iterable save(Iterable entities);" method? Also, since it looks like the method that accepts an iterator just calls entity.persist() anyway, is there any downside to me just calling save(U entity) in a foreach loop anyway?