em.close();
em = ResourceUtils.getEntityManager();
em.getTransaction().begin();
ApplicationOutput applicationOutput = new ApplicationOutput();
TaskDetail taskDetail = em.find(TaskDetail.class, taskDetailResource.getTaskId());
applicationOutput.setTask(taskDetail);
applicationOutput.setTaskId(taskDetail.getTaskId());
applicationOutput.setOutputKey(outputKey);
applicationOutput.setOutputKeyType(outputType);
applicationOutput.setValue(value);
applicationOutput.setMetadata(metadata);
if (existingOutput != null) {
existingOutput.setTask(taskDetail);
existingOutput.setTaskId(taskDetail.getTaskId());
existingOutput.setOutputKey(outputKey);
existingOutput.setOutputKeyType(outputType);
existingOutput.setValue(value);
existingOutput.setMetadata(metadata);
applicationOutput = em.merge(existingOutput);