Flowable introductory series article 70 - Introduction to JPA

Keywords: Java Flowable oa bpm

1. Description

You can use JPA entities as process variables, allowing you to:

  • Update the existing JPA entity based on process variables that can be filled in the userTask or generated in the serviceTask.
  • Reuse existing domain models without having to write explicit services to get entities and update values
  • Make a decision based on the attributes of an existing entity (Gateway).
  • ...

2. Request

Only entities that meet the following conditions are supported:

  • Entities should be configured using JPA annotations, and we support both field and attribute access. Mapped superclasses can also be used.
  • The entity should have a primary key annotation @ Id, and composite primary keys are not supported (@ EmbeddedId and @ IdClass). The Id field / attribute can be any type supported in the JP-A-specification: basic types and their wrappers (excluding Boolean values), String,, BigInteger and BigDecimaljava.util.Datejava.sql.Date.

3. Configuration

In order to be able to use JPA entities, the engine must have a reference EntityManagerFactory. This can be done by configuring a reference or providing a persistence unit name. JPA entities used as variables will be automatically detected and processed accordingly.

The following example configuration uses jpaprersistenceunitname:

<bean id="processEngineConfiguration"
<!-- Database configurations -->
<property name="databaseSchemaUpdate" value="true" />
<property name="jdbcUrl" value="jdbc:h2:mem:JpaVariableTest;DB_CLOSE_DELAY=1000" />
<property name="jpaPersistenceUnitName" value="flowable-jpa-pu" />
<property name="jpaHandleTransaction" value="true" />
<property name="jpaCloseEntityManager" value="true" />
<!-- job executor configurations -->
<property name="jobExecutorActivate" value="false" />
<!-- mail server configurations -->
<property name="mailServerPort" value="5025" />

The following example configuration provides an EntityManagerFactory that we define ourselves (in this case, an open jpa entity manager). Note that the fragment contains only the bean s related to the example, and the rest is omitted. A complete example of using the open jpa entity manager can be found in flowable spring examples (/ flowable spring / SRC / test / Java / org / flowable / spring / test / jpa / jpaspringtest. Java).

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="pum"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
<property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.H2Dictionary" />
<bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jpaEntityManagerFactory" ref="entityManagerFactory" />
<property name="jpaHandleTransaction" value="true" />
<property name="jpaCloseEntityManager" value="true" />
<property name="jobExecutorActivate" value="false" />

The same configuration can also be performed when building the engine programmatically, for example:

ProcessEngine processEngine = ProcessEngineConfiguration

Configuration properties:

  • Jpaprersistenceunitname: the name of the persistence unit to use. (please ensure that the persistence unit is available in the classpath. According to the specification, the default location is / METAINF/persistence.xml). Use jpaEntityManagerFactory or jpapexistenceunitname.
  • jpaEntityManagerFactory: the reference to the bean that implements javax.persistence.EntityManagerFactory will be used to load the entity and refresh the update. Use jpaEntityManagerFactory or jpapexistenceunitname.
  • jpaHandleTransaction: flag indicating that the engine should start and commit / rollback transactions on the EntityManager instance used. Set to false when using the Java Transaction API (JTA).
  • jpaCloseEntityManager: Flag EntityManagerFactory indicating that the engine should shut down the instance obtained from EntityManager. Set to false if the EntityManager is container managed (for example, when using an extended persistence context instead of the scope of a single transaction).

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:

Posted by CaptainStarbuck on Sun, 28 Nov 2021 16:55:14 -0800