盘古BPM体验地址    盘古BPM交流群盘古BPM交流群号:963222735

Flowable实战-JPA

分享牛 3669℃


1. Flowable实战-JPA

可以使用JPA实体作为流程变量,这样可以:

  • 基于流程变量更新已有JPA实体。流程变量可以在用户任务的表单中填写,或者通过服务任务生成。

  • 重用已有的领域模型,而不需要写专门的服务用于读取与更新实体值。

  • 基于已有实体做决策(网关)。

1.1. 需求 Requirements

只能支持完全满足下列条件的实体:

  • 实体需要使用JPA注解配置,字段与参数访问器都支持。也可以使用映射的父类。

  • 实体需要有使用@Id注解的主键,不支持复合主键(@EmbeddedId@IdClass)。Id字段/参数可以是任何JPA规范支持的类型:原生类型与其包装器(除了boolean)、StringBigIntegerBigDecimaljava.util.Datejava.sql.Date

1.2 配置 Configuration

要使用JPA实体,引擎必须引用EntityManagerFactory。可以通过配置引用,或者提供持久化单元名(Persistence Unit Name)来实现。用作变量的JPA实体将将被自动检测,并会按情况处理。

下面的示例配置使用jpaPersistenceUnitName:

<bean id="processEngineConfiguration"
  class="org.flowable.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
        <!-- 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" />
</bean>

下面的示例配置提供了我们自己定义的EntityManagerFactory(在这个例子里,是一个open-jpa实体管理器)。请注意这段代码只包含了与本例相关的bean,省略了其他的。带有open-jpa实体管理器的完整的可用示例,可以在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>
  </property>
</bean>
<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" />
</bean>

也可以在编程构建引擎时,使用相同的配置,例如:

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

配置参数:

  • jpaPersistenceUnitName:要使用的持久化单元的名字。(要确保该持久化单元在classpath中可用。根据规范,默认位置为/META-INF/persistence.xml)。jpaEntityManagerFactoryjpaPersistenceUnitName二选一。

  • jpaEntityManagerFactory:对实现了javax.persistence.EntityManagerFactory的bean的引用,将用于载入实体,并刷入更新。jpaEntityManagerFactoryjpaPersistenceUnitName二选一。

  • jpaHandleTransaction:标示引擎是否需要启动事务,并在使用EntityManager实例后提交/回滚。当使用Java Transaction API (JTA)时,设置为false。

  • jpaCloseEntityManager:标示引擎是否需要关闭其从EntityManagerFactory获取的EntityManager实例。当EntityManager由容器管理时(例如,使用扩展持久化上下文 Extended Persistence Context时,不支持将范围限制为单一事务)设置为false。

转载请注明:分享牛 » Flowable实战-JPA