Flowable introductory series Article 72 - JPA advanced usage

Posted by RestlessThoughts on Wed, 01 Dec 2021 02:28:45 +0100

1. Query JPA process variables

You can query ProcessInstances and Executions with specific JPA entities as variable values. Note that only variableValueEquals(name, entity) supports ProcessInstanceQuery and ExecutionQuery for JPA entities. The methods variableValueNotEquals, variableValueGreaterThan, variableValueGreaterThan orequal, variableValueLessThan and variableValueLessThan orequal are not supported. When a FlowableException is thrown, the JPA entity is passed as a value.

ProcessInstance result = runtimeService.createProcessInstanceQuery()
.variableValueEquals("entityToQuery", entityToQuery).singleResult();

2. Advanced examples using spring beans and JPA

For a more advanced example, JPA springtest, you can find flowable spring examples here. It describes the following simple use cases:

  • Existing spring beans using JPA entities already exist, allowing loan requests to be stored.
  • With Flowable, we can use existing entities, get them through existing bean s, and use them as variables in our process. The process is defined in the following steps:
    • The service task creates a new LoanRequest and uses the existing usage variables received when the loanrequesbean starts the process (for example, it may come from a start form). The created entity is stored as a variable, and the expression result is stored as a variable using flowable:resultVariable.
    • UserTask allows the manager to review the request and approve / disapprove it, storing it as a boolean variable approvedByManager.
    • ServiceTask updates the loan application entity so that the entity is synchronized with the process.
    • Depending on the value approved of the entity attribute, the exclusive gateway is used to determine the path to be taken in the next step: when the request is approved, the process ends, otherwise, an additional task will become available (send a rejection letter), so the customer can manually notify by rejecting the letter.

Note that this procedure does not contain any form, as it is only used for unit testing.

<?xml version="1.0" encoding="UTF-8"?>
<definitions id="taskAssigneeExample"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="org.flowable.examples">
<process id="LoanRequestProcess" name="Process creating and handling loan request">
<startEvent id='theStart' />
<sequenceFlow id='flow1' sourceRef='theStart' targetRef='createLoanRequest' />
<serviceTask id='createLoanRequest' name='Create loan request'
flowable:expression="${loanRequestBean.newLoanRequest(customerName, amount)}"
flowable:resultVariable="loanRequest"/>
<sequenceFlow id='flow2' sourceRef='createLoanRequest' targetRef='approveTask' />
<userTask id="approveTask" name="Approve request" />
<sequenceFlow id='flow3' sourceRef='approveTask' targetRef='approveOrDissaprove' />
<serviceTask id='approveOrDissaprove' name='Store decision'
flowable:expression="${loanRequest.setApproved(approvedByManager)}" />
<sequenceFlow id='flow4' sourceRef='approveOrDissaprove' targetRef='exclusiveGw' />
<exclusiveGateway id="exclusiveGw" name="Exclusive Gateway approval" />
<sequenceFlow id="endFlow1" sourceRef="exclusiveGw" targetRef="theEnd">
<conditionExpression xsi:type="tFormalExpression">${loanRequest.approved}</conditionExpression>
</sequenceFlow>
<sequenceFlow id="endFlow2" sourceRef="exclusiveGw" targetRef="sendRejectionLetter">
<conditionExpression xsi:type="tFormalExpression">${!loanRequest.approved}</conditionExpression>
</sequenceFlow>
<userTask id="sendRejectionLetter" name="Send rejection letter" />
<sequenceFlow id='flow5' sourceRef='sendRejectionLetter' targetRef='theOtherEnd' />
<endEvent id='theEnd' />
<endEvent id='theOtherEnd' />
</process>
</definitions>

Although the above example is simple, it shows the power of using JPA and Spring and parameterized method expressions. This process does not require custom java code (except Spring bean s) and greatly accelerates development.

The above article is from Pangu BPM Research Institute: http://vue.pangubpm.com/
Article translation submission: https://github.com/qiudaoke/flowable-userguide
For more articles, you can focus on WeChat official account:

Topics: Java Flowable oa bpm