方式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); }