I have a repository interface parameterized with stored entities type. Among other methods in it, I have two of interest: the create()
method which instantiates an entity, and the save()
method that saves the entity:
public interface INamedEntityRepository<T extends INamedEntity> {
T create(String name);
void save(T namedEntity);
}
...
Next follows a snippet of how this interface is used. I am getting a compilation error saying the type returned from the create()
and that passed into save()
are incompatible.
INamedEntityRepository<? extends INamedEntity> repo = getEntityRepository();
INamedEntity toSave = repo.create("Named");
... // configure the entity more...
repo.save(toSave);
^
The method save(capture#7-of ? extends INamedEntity) in the type INamedEntityRepository<capture#7-of ? extends INamedEntity> is not applicable for the arguments (INamedEntity)
. I can understand this, because really INamedEntity
is not necessarily the expected type.
But even doing this
repo.save(repo.create("Named")));
won't help:
The method save(capture#7-of ? extends INamedEntity) in the type INamedEntityRepository<capture#7-of ? extends INamedEntity> is not applicable for the arguments (capture#8-of ? extends INamedEntity)
.
What's the problem? How to correctly handle this situation? Thanks in advance.