The usage of two bounded wildcards with the same bound doesn't mean they are the same type. In this case, the actual type argument for ActivityDao<? extends BaseActivity>
might not be same as the one coming with Collection<? extends BaseActivity>
. So, the saveAll()
method wouldn't work.
To elaborate further, the compiler replaces each wildcard occurrence with a unique placeholder which is nothing but capture of that wildcard. That means, Collection<?>
becomes Collection<CAP#1 of ?>
, similarly Collection<? extends T>
becomes Collection<CAP#1 of ? extends T>
. If a Collection<?>
is again encountered, it will be replaced with Collection<CAP#2 of ?>
.
So, your method declaration is somewhat similar to:
private void persistData(ActivityDao<CAP#1-of-? extends BaseActivity> activityDao, Collection<CAP#2-of-? extends BaseActivity> data){
EntityTransaction transaction = activityDao.getEntityManager().getTransaction();
try {
transaction.begin();
activityDao.saveAll(data);
transaction.commit();
}
catch (HibernateException ex) {
}
}
where CAP#1
and CAP#2
are different type parameters for compiler. Of course, the signature of saveAll
for that method becomes like this:
public void saveAll(Collection<CAP#1-of-? extends BaseActivity> objects) throws PersistenceException;
And since Collection<T1>
is not the same as Collection<T2>
for two different type arguments, so that method invocation fails.
You can resolve this issue by using generic method instead:
private <T extends BaseActivity> void persistData(ActivityDao<T> activityDao, Collection<T> data){