diff --git a/execution/src/main/java/org/springframework/batch/execution/repository/dao/SqlJobDao.java b/execution/src/main/java/org/springframework/batch/execution/repository/dao/SqlJobDao.java index f14dd1a8b..7e5fd60f3 100644 --- a/execution/src/main/java/org/springframework/batch/execution/repository/dao/SqlJobDao.java +++ b/execution/src/main/java/org/springframework/batch/execution/repository/dao/SqlJobDao.java @@ -26,6 +26,7 @@ import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobIdentifier; import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.repository.NoSuchBatchDomainObjectException; +import org.springframework.batch.execution.runtime.DefaultJobIdentifier; import org.springframework.batch.execution.runtime.ScheduledJobIdentifier; import org.springframework.batch.repeat.ExitStatus; import org.springframework.beans.factory.InitializingBean; @@ -372,6 +373,10 @@ public class SqlJobDao implements JobDao, InitializingBean { if (jobIdentifier instanceof ScheduledJobIdentifier) { return (ScheduledJobIdentifier) jobIdentifier; } + if (jobIdentifier instanceof DefaultJobIdentifier) { + return new ScheduledJobIdentifier(jobIdentifier.getName(), + ((DefaultJobIdentifier) jobIdentifier).getJobKey()); + } return new ScheduledJobIdentifier(jobIdentifier.getName()); } diff --git a/execution/src/main/java/org/springframework/batch/execution/runtime/DefaultJobIdentifier.java b/execution/src/main/java/org/springframework/batch/execution/runtime/DefaultJobIdentifier.java index 8a430be55..71f7ee74a 100644 --- a/execution/src/main/java/org/springframework/batch/execution/runtime/DefaultJobIdentifier.java +++ b/execution/src/main/java/org/springframework/batch/execution/runtime/DefaultJobIdentifier.java @@ -43,6 +43,14 @@ public class DefaultJobIdentifier extends SimpleJobIdentifier implements super(name); } + /** + * @param name the name for the job + */ + public DefaultJobIdentifier(String name, String key) { + this(name); + this.key = key; + } + public String getJobKey() { return key; } diff --git a/execution/src/main/java/org/springframework/batch/execution/runtime/ScheduledJobIdentifier.java b/execution/src/main/java/org/springframework/batch/execution/runtime/ScheduledJobIdentifier.java index 72faf7e53..e7cfc11a6 100644 --- a/execution/src/main/java/org/springframework/batch/execution/runtime/ScheduledJobIdentifier.java +++ b/execution/src/main/java/org/springframework/batch/execution/runtime/ScheduledJobIdentifier.java @@ -37,6 +37,19 @@ public class ScheduledJobIdentifier extends DefaultJobIdentifier implements JobI public ScheduledJobIdentifier(String name) { super(name); + initDate(); + } + + /** + * @param name + * @param key + */ + public ScheduledJobIdentifier(String name, String key) { + super(name, key); + initDate(); + } + + private void initDate() { try { scheduleDate = dateFormat.parse("19700101"); } catch (ParseException e) { diff --git a/execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java b/execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java index 2e0a9d964..33c1a3cba 100644 --- a/execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java @@ -26,6 +26,7 @@ import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.repository.NoSuchBatchDomainObjectException; import org.springframework.batch.core.runtime.SimpleJobIdentifier; +import org.springframework.batch.execution.runtime.DefaultJobIdentifier; import org.springframework.batch.execution.runtime.ScheduledJobIdentifier; import org.springframework.batch.repeat.ExitStatus; import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; @@ -224,13 +225,26 @@ public abstract class AbstractJobDaoTests extends // Create job. job = jobDao.createJob(jobIdentifier); - // sessionFactory.getCurrentSession().flush(); + List jobs = jdbcTemplate.queryForList( + "SELECT * FROM BATCH_JOB where ID=?", new Object[] { job + .getId() }); + assertEquals(1, jobs.size()); + assertEquals(job.getName(), ((Map) jobs.get(0)).get("JOB_NAME")); + + } + + public void testJobWithDefaultJobIdentifier() throws Exception { + DefaultJobIdentifier jobIdentifier = new DefaultJobIdentifier("Job1", "testKey"); + + // Create job. + job = jobDao.createJob(jobIdentifier); List jobs = jdbcTemplate.queryForList( "SELECT * FROM BATCH_JOB where ID=?", new Object[] { job .getId() }); assertEquals(1, jobs.size()); assertEquals(job.getName(), ((Map) jobs.get(0)).get("JOB_NAME")); + assertEquals(jobIdentifier.getJobKey(), ((Map) jobs.get(0)).get("JOB_KEY")); }