0

I have a List which are include 'Job' Entities. So I need to insert to database that List as a batch without iterating. what is the best way to do that?

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public JobResult save(JobDTO dto) throws Exception {

    JobResult result = new JobResult(new Job(), dto);
    try {
        JobMapper.getInstance().dtoToDomain(result.getDtoEntity(), result.getDomainEntity());
        setBatchJobData(result);
        result.addToMessageList("Jobs Added Successfully.");
        result.updateDtoIdAndVersion();
    } catch (Exception ex) {
        ex.printStackTrace();
        result.setResultStatusError();
        result.addToErrorList(ex.getMessage());
    }
    return result;

}

private void setBatchJobData(JobResult result) throws Exception{
    List<Job> jobs = new ArrayList<>();
    for (Integer scheduleTaskId : result.getDtoEntity().getScheduleTaskIds()) {
        Job job = new Job();
        AgreementScheduleTask agreementScheduleTask = agreementScheduleTaskDao.findOne(scheduleTaskId);
        setJobData(job, agreementScheduleTask);
        setAssets(job, agreementScheduleTask);
        jobs.add(job);
    }   

}
2
  • You want a solution that iterate for you ? Because at some point that list need to be iterated.. Commented Aug 31, 2017 at 9:37
  • stackoverflow.com/questions/20458401/… Commented Aug 31, 2017 at 9:40

1 Answer 1

2

If you are using spring,

  1. use map instead of List. Map<String, Job> jobs = new HashMap<>();

  2. Add the Jobs to the Map.

  3. save at one go : jobRepository.save(jobs)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.