方式1:
在保存每一個流程實例時,設置多個流程變量,通過多個流程變量的組合來過濾篩選符合該組合條件的流程實例,以后在需要查詢對應業務對象所對應的流程實例時,只需查詢包含該流程變量的值的流程實例即可.
設置過程:
public void startProcess(Long id) { Customer cus = get(id); if (cus != null) { // 修改客戶的狀態 cus.setStatus(1); updateStatus(cus); // 將客戶的追蹤銷售員的nickName放入流程變量,該seller變量可用於查詢包括該seller的流程實例 Map<String, Object> map = new HashMap<String, Object>(); if (cus.getSeller() != null) { map.put("seller", cus.getSeller().getNickname()); } // 將客戶的類型和id放入流程變量,此2個流程變量可用於查詢該客戶對象所對應的流程實例 String classType = cus.getClass().getSimpleName(); map.put("classType", classType); map.put("objId", id); // 獲取processDefinitionKey,默認為類型簡單名稱加上Flow String processDefinitionKey = classType + "Flow"; // 開啟流程實例 workFlowService.startProcessInstanceByKeyAndVariables(processDefinitionKey, map); } }
查詢過程:
/** * 測試根據變量值的限定獲取相應的流程實例 * * @throws Exception */ @Test public void testGetFromVariable() throws Exception { Employee sller = new Employee(); sller.setNickname("員工2"); sller.setId(4L); List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery() .variableValueEquals("classType", sller.getClass().getSimpleName()) .list(); System.out.println(processInstances); for (ProcessInstance processInstance : processInstances) { System.out.println(processInstance); } //===================================================================================================== Customer cus = new Customer(); cus.setId(4L); List<ProcessInstance> processInstances1 = runtimeService.createProcessInstanceQuery() .variableValueEquals("classType", cus.getClass().getSimpleName()) .variableValueEquals("objId", cus.getId()).list(); System.out.println(processInstances); for (ProcessInstance processInstance : processInstances1) { System.out.println(processInstance); } }
方式2:
使用 businessKey,在開啟流程實例時設置businessKey作為業務對象關聯流程實例的關聯鍵
設置過程:
/** * 使用businessKey作為流程實例關聯業務對象的關聯鍵 * * @throws Exception */ @Test public void testBusKey() throws Exception { //設置businessKey Customer customer = new Customer(); customer.setId(2L); //businessKey采用簡單類名+主鍵的格式 String busniessKey = customer.getClass().getSimpleName() + customer.getId(); String definitionKey = customer.getClass().getSimpleName() + "Flow"; Map<String, Object> map = new HashMap<String, Object>(); map.put("seller", "admin"); //開啟流程 runtimeService.startProcessInstanceByKey(definitionKey, busniessKey, map); }
查詢過程:
/** * 使用businessKey查詢相應業務對象的流程實例 * * @throws Exception */ @Test public void testgetByBusKey() throws Exception { List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery() .processInstanceBusinessKey("Customer2", "CustomerFlow").list(); System.out.println(processInstances); }