Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure workflow engine Activiti integrate JPA persistence to save process data

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to configure workflow engine Activiti to integrate JPA persistence to save process data", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to configure workflow engine Activiti integrated JPA persistence to save process data"!

Brief introduction of JPA in Activiti

You can use JPA entities as process variables and operate:

Update existing JPA entities based on process variables, which can be filled in the form of the user task or generated by the service task

Reuse existing domain models without writing displayed services to acquire entities or update entity values

Make a judgment based on the attributes of an existing entity (gateway is branch aggregation)

JPA entity requirement

JPA in Activiti supports only entities that meet the following requirements:

Id fields or properties can use any type supported by the JPA specification:

Original ecological data types and their packaging types (except Boolean)

String

BigInteger

BigDecimal

Java.util.Date

Java.sql.Date

Entities should be configured using JPA annotations to support both field and property access. @ MappedSuperclass should also be able to be used

There should be a primary key in the entity that is annotated with @ Id. Compound primary keys @ EmbeddedId and @ IdClass are not supported:

JPA configuration

The engine must have a reference to the EntityManagerFactory to use the JPA entity, so that you can configure the reference or provide a persistence unit name

JPA entities as variables will be automatically detected and processed accordingly

Configure using jpaPersistenceUnitName:

Configure a custom EntityManagerFactory

The OpenJPA entity Manager is used here

This code snippet contains only the beans associated with the example, excluding other beans.

Complete and usable examples of OpenJPA entity management can be found in activiti-spring-examples (/ activiti-spring/src/test/java/org/activiti/spring/test/jpa/JPASpringTest.java)

You can also configure when the programmer creates an engine:

ProcessEngine processEngine = ProcessEngineConfiguration .createProcessEngineConfigurationFromResourceDefault () .setJpaPersistenceUnitName ("activiti-pu") .buildProcessEngine ()

The configured properties are:

JpaPersistenceUnitName: use the name of the persistence unit:

To ensure that the persistence unit is available under the classpath, the default path is / META-INF/persistence.xml

Either use jpaEntityManagerFactory or jpaPersistenceUnitName

JpaEntityManagerFactory: a reference to a bean that implements javax.persistence.EntityManagerFactory:

Will be used to load entities and refresh updates

Either use jpaEntityManagerFactory or jpaPersistenceUnitName

JpaHandleTransaction: on the EntityManager instance being used, this flag indicates whether the process engine needs to start and commit or roll back the transaction:

Set to false when using Java transaction API (JTA)

JpaCloseEntityManager: this flag indicates whether the process engine should close the instance of EntityManager obtained from EntityManagerFactory:

Set to false when EntityManager is container managed: when using an extended persistence context that is not a single transaction scope

Simple example of JPA usage

First, you need to create a META-INF/persistence.xml-based EntityManagerFactory as a persistence unit: containing all the classes in the persistence unit and some vendor-specific configurations

Use a simple entity as a test, which contains a value attribute of type id and String, which will also be persisted

Before testing, create an entity and save:

Entity (name = "JPA_ENTITY_FIELD") public class FieldAccessJPAEntity {@ Id @ Column (name = "ID_") private Long id; private String value; public FieldAccessJPAEntity () {/ / Empty constructor needed for JPA} public Long getId () {return id;} public void setId (Long id) {this.id = id;} public String getValue () {return value;} public void setValue (String value) {this.value = value;}

Start a new process instance and add an entity as a variable. Other variables will be stored in the process engine's persistent database. The next time you get this variable, it will be loaded from the EntityManager based on the class and the storage Id:

Map variables = new HashMap (); variables.put ("entityToUpdate", entityToUpdate); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey ("UpdateJPAValuesProcess", variables)

The first node in the process definition is a service task, which will call the setValue method on entityToUpdate, which is actually the JPA variable that was set when the process instance was started and will be loaded from the context-sensitive EntityManager of the current process engine:

When the service task is completed, the process instance will stay on the user task link defined in the process definition:

You can view the process instance

The EntityManager has been refreshed and the changed entities have been saved in the database

When you get the variable value of entityToUpdate, the entity will be loaded again and the value of the entity attribute will be updatedValue

/ / Servicetask in process' UpdateJPAValuesProcess' should have set value on entityToUpdate.Object updatedEntity = runtimeService.getVariable (processInstance.getId (), "entityToUpdate"); assertTrue (updatedEntity instanceof FieldAccessJPAEntity); assertEquals ("updatedValue", ((FieldAccessJPAEntity) updatedEntity). GetValue () query JPA process variables

ProcessInstances and Executions with querying a JPA entity as a variable

Only variableValueEquals (name, entity) supports JPA entity variables in ProcessInstanceQuery and ExecutionQuery queries:

[variableValueNotEquals], [variableValueGreaterThan], [variableValueGreaterThanOrEqual], [variableValueLessThan], [variableValueLessThanOrEqual] are not supported and an ActivitiException is thrown when the JPA entity value is passed

ProcessInstance result = runtimeService.createProcessInstanceQuery () .variableValueEquals ("entityToQuery", entityToQuery) .singleResult (); advanced example of using a combination of Spring beans and JPA

JPASpringTest, in activiti-spring-examples:

There is already a Spring-bean that uses JPA entities to store loan applications

With Activiti, you can get an entity that is already in use through an existing bean and use it as a variable in the process

Process definition steps:

When the application is approved, the process ends

Otherwise, an additional task will be used (send a rejection letter) so that the customer can be notified manually.

Update the loan application entity so that it is synchronized with the process

Allow the manager to review the loan application and fill in the approval comments (agree / disagree)

Approval comments will be stored as a boolean variable approvedByManager

Create a new loan application and accept the variables from the startup process using the existing LoanRequestBean (from the form at the start of the process)

Use activiti:resultVariable, which stores the result returned by the expression as a variable, to store the created entity as a variable

Service tasks:

User tasks:

Service tasks:

According to the value of the loan application entity variable approved, the unique gateway will be used to automatically determine which path to choose next:

${loanRequest.approved} ${! loanRequest.approved} Thank you for your reading. The above is the content of "how to configure workflow engine Activiti to integrate JPA persistence to save process data". After the study of this article, I believe you have a deeper understanding of how to configure workflow engine Activiti to integrate JPA persistence to save process data. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report