public JobInstance createJobInstance(String jobName, final JobParameters jobParameters) {
    Assert.notNull(jobName, "Job name must not be null.");
    Assert.notNull(jobParameters, "JobParameters must not be null.");

    Assert.state(
        getJobInstance(jobName, jobParameters) == null, "JobInstance must not already exist");

    Long jobId = getNextId(JobInstance.class.getSimpleName(), mongoTemplate);

    JobInstance jobInstance = new JobInstance(jobId, jobName);

    jobInstance.incrementVersion();

    Map<String, JobParameter> jobParams = jobParameters.getParameters();
    Map<String, Object> paramMap = new HashMap<String, Object>(jobParams.size());
    for (Map.Entry<String, JobParameter> entry : jobParams.entrySet()) {
      paramMap.put(
          entry.getKey().replaceAll(DOT_STRING, DOT_ESCAPE_STRING), entry.getValue().getValue());
    }
    getCollection()
        .save(
            start()
                .add(JOB_INSTANCE_ID_KEY, jobId)
                .add(JOB_NAME_KEY, jobName)
                .add(JOB_KEY_KEY, createJobKey(jobParameters))
                .add(VERSION_KEY, jobInstance.getVersion())
                .add(JOB_PARAMETERS_KEY, new BasicDBObject(paramMap))
                .get());
    return jobInstance;
  }
  private JobInstance mapJobInstance(DBObject dbObject, JobParameters jobParameters) {
    JobInstance jobInstance = null;
    if (dbObject != null) {
      Long id = (Long) dbObject.get(JOB_INSTANCE_ID_KEY);
      if (jobParameters == null) {
        jobParameters = getJobParameters(id, mongoTemplate);
      }

      jobInstance =
          new JobInstance(
              id,
              (String)
                  dbObject.get(
                      JOB_NAME_KEY)); // should always be at version=0 because they never get
                                      // updated
      jobInstance.incrementVersion();
    }
    return jobInstance;
  }