JobDAO { public void termJob(Integer processId) { Session session = sessionFactory.openSession(); try { session.beginTransaction(); Process parentProcessId = new Process(); parentProcessId.setProcessId(processId); List<Process> listOfSubProcesses = new ArrayList<Process>(); Criteria listOfSubProcessCriteria = session.createCriteria(Process.class).add(Restrictions.eq(PROCESS, parentProcessId)).add(Restrictions.eq(DELETEFLAG, false)); for (Object subProcessObject : listOfSubProcessCriteria.list()) { Process subProcess = (Process) subProcessObject; listOfSubProcesses.add(subProcess); } ExecStatus runningExecStatus = new ExecStatus(); runningExecStatus.setExecStateId(2); ExecStatus failedExecStatus = new ExecStatus(); failedExecStatus.setExecStateId(6); ExecStatus successExecStatus = new ExecStatus(); successExecStatus.setExecStateId(3); BatchStatus newBatchStatus = new BatchStatus(); newBatchStatus.setBatchStateId(0); BatchStatus processedBatchStatus = new BatchStatus(); processedBatchStatus.setBatchStateId(1); Criteria validProcessCriteria = session.createCriteria(Process.class).add(Restrictions.eq(PROCESSID, processId)).add(Restrictions.eq(DELETEFLAG, false)); if (validProcessCriteria.list().isEmpty()) { LOGGER.error(INVALIDPARENTPROCESS + processId); throw new MetadataException(INVALIDPARENTPROCESS + processId); } else { Process validProcess = (Process) validProcessCriteria.list().get(0); if (validProcess.getProcess() != null) { LOGGER.error(INVALIDPARENTPROCESS + processId); throw new MetadataException(INVALIDPARENTPROCESS + processId); } } Criteria runningProcessCriteria = session.createCriteria(InstanceExec.class).add(Restrictions.eq(PROCESS, parentProcessId)) .addOrder(Order.desc("startTs")).setMaxResults(1); if (!runningProcessCriteria.list().isEmpty()) { InstanceExec runningInstanceExec = (InstanceExec) runningProcessCriteria.list().get(0); if (runningInstanceExec.getExecStatus().getExecStateId() != 2) { LOGGER.error(NOTUNDEREXECUTIONPROCESS + processId); throw new MetadataException(NOTUNDEREXECUTIONPROCESS + processId); } } if (!listOfSubProcesses.isEmpty()) { Criteria runningSubProcessCriteria = session.createCriteria(InstanceExec.class).add(Restrictions.in(PROCESS, listOfSubProcesses)) .add(Restrictions.eq(EXECSTATUS, runningExecStatus)); if (!runningSubProcessCriteria.list().isEmpty()) { LOGGER.error(" sub process in running state: "+runningSubProcessCriteria.list()); throw new MetadataException("sub process in running state"); } } Criteria updateInstanceExecCriteria = session.createCriteria(InstanceExec.class).add(Restrictions.eq(PROCESS, parentProcessId)) .add(Restrictions.eq(EXECSTATUS, runningExecStatus)); InstanceExec updateInstanceExec = (InstanceExec) updateInstanceExecCriteria.list().get(0); updateInstanceExec.setEndTs(new Date()); updateInstanceExec.setExecStatus(failedExecStatus); session.update(updateInstanceExec); session.getTransaction().commit(); } catch (MetadataException e) { session.getTransaction().rollback(); LOGGER.error(e); throw e; } finally { session.close(); } }  List<InitJobRowInfo> initJob(Integer processId, Integer maxBatch); void haltJob(Integer processId, String batchMarking); void termJob(Integer processId);  }
@Ignore @Test public void testTermJob() throws Exception { jobDAO.termJob(1); }