Mode 1:
When you save each process instance, you set up multiple process variables, filter the process instances that match that combination by combining multiple process variables, and later, when you need to query the process instance that corresponds to the business object, simply query the process instance that contains the value of the process variable.
Setup process:
Public voidstartprocess (Long id) {Customer cus=get (ID); if(Cus! =NULL) { //Modify the status of a customerCus.setstatus (1); UpdateStatus (cus); // nickname The customer's tracking salesperson into the process variable, which can be used to query the process instance that includes the seller map<string, object> map =NewHashmap<string, object>(); if(Cus.getseller ()! =NULL) {Map.put ("Seller", Cus.getseller (). Getnickname ()); } // put the type and ID of the customer into the process variable, which can be used to query the process instance corresponding to the Customer object for the 2 process variablesString ClassType =Cus.getclass (). Getsimplename (); Map.put ("ClassType", ClassType); Map.put ("ObjId", id); //get Processdefinitionkey, default to type simple name plus flowString Processdefinitionkey = ClassType + "Flow"; //Open Process Instanceworkflowservice.startprocessinstancebykeyandvariables (Processdefinitionkey, map); } }
query process:
/*** Test the corresponding process instance according to the limit of the variable value * *@throwsException*/@Test Public voidTestgetfromvariable ()throwsException {Employee Sller=NewEmployee (); Sller.setnickname ("Staff 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 =NewCustomer (); 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); } }
Mode 2:
Use Businesskey to set Businesskey as the association key for the business object Association Process instance when the process instance is opened
Setup Process :
/*** Use Businesskey as the Association key of the business object associated with the process instance * *@throwsException*/@Test Public voidTestbuskey ()throwsException {//Set BusinesskeyCustomer customer =NewCustomer (); Customer.setid (2L); //Businesskey format with simple class name + primary keyString Busniesskey = Customer.getclass (). Getsimplename () +Customer.getid (); String Definitionkey= Customer.getclass (). Getsimplename () + "Flow"; Map<string, object> map =NewHashmap<string, object>(); Map.put ("Seller", "admin"); //Open ProcessRuntimeservice.startprocessinstancebykey (Definitionkey, Busniesskey, map); }
Query process:
/** * Use Businesskey to query the process instance of the corresponding business object * @throws Exception */ @Test publicvoidthrows Exception { List <ProcessInstance> processinstances = runtimeservice.createprocessinstancequery () . Processinstancebusinesskey ("Customer2", "Customerflow"). List (); System.out.println (processinstances); }
Activiti 2 ways to bind the corresponding business object for each process