@Override public List<ID> upsert(Iterable<T> dEntities) { final TransactionStatus transactionStatus = getTransaction(); // group entities by create or update: final ArrayList<T> toCreate = new ArrayList<T>(); final ArrayList<T> toUpdate = new ArrayList<T>(); ID id; for (T d : dEntities) { id = dao.getSimpleKey(d); if (null == id) { toCreate.add(d); } else { toUpdate.add(d); } } LOG.debug("Creating {}, Updating {}", toCreate.size(), toUpdate.size()); LOG.debug("Creating {}, Updating {}", toCreate, toUpdate); try { // create new entities using async API Future<List<?>> createFuture = null; if (!toCreate.isEmpty()) { createFuture = dao.persistForFuture(toCreate); } // update in parallel if (!toUpdate.isEmpty()) { dao.update(toUpdate); } // join future if (null != createFuture) { Collection<ID> ids = dao.getSimpleKeys(createFuture); // Iterator<ID> i = ids.iterator(); // for (T t : toCreate) { // dao.setSimpleKey(t, i.next()); // } } // collect the IDs final ArrayList<ID> body = new ArrayList<ID>(); for (T d : dEntities) { body.add(getSimpleKey(d)); } commitTransaction(transactionStatus); return body; } finally { rollbackTransaction(transactionStatus); } }