AnsweredAssumed Answered

Java service task, JavaDelegate JPA

Question asked by nexowski on Jun 11, 2014
Latest reply on Jun 20, 2014 by nexowski
Hi,

I couldn't find information about how to create a JPA entity in Java ServiceTask. I can do it before starting the process by adding it as a process variable:

ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);


Well I want activiti to manage the creation of entities when in ServiceTasks, how can I achieve that, I'm not using spring (or am i? ), just JPA.

my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
      <persistence-unit name="DAO">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>           
      
      …my classes…
      
      <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/orderbase" />
            <property name="hibernate.connection.username" value="user" />
            <property name="hibernate.connection.password" value="pwd1" />
            <property name="hibernate.hbm2ddl.auto" value="update" />     
        </properties>
      
   </persistence-unit>
            
</persistence>


I configure my process Engine like this:


ProcessEngineConfigurationImpl pEC = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
      pEC.setJpaPersistenceUnitName("DAO")
         .setJpaHandleTransaction(true)
         .setDatabaseSchemaUpdate("false")
         .setJdbcDriver("com.mysql.jdbc.Driver")
         .setJdbcUrl("jdbc:mysql://localhost:3306/activiti")
         .setJobExecutorActivate(false)
         .setJdbcUsername("user")
         .setJdbcPassword("pwd");   
      
      processEngine = pEC.buildProcessEngine();


I get entityManager like this:

EntityManagerSessionFactory entityManagerSessionFactory = (EntityManagerSessionFactory) pEC
                 .getSessionFactories()
                 .get(EntityManagerSession.class);
              
      entityManagerFactory = entityManagerSessionFactory.getEntityManagerFactory();


And I can persist the entity:

EntityManager manager = entityManagerFactory.createEntityManager();
       manager.getTransaction().begin();
       Order newOrder=new Order();      
…doing stuff here order related….
      manager.persist(newOrder);
      manager.flush();
      manager.getTransaction().commit();
      manager.close();
      variables.put("order", newOrder);
      
       ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);


And it works perfectly, I can retrieve the order throughout the process, every change in ServiceTasks persists it to Database(not the same as activiti).

But, there's always a but :) I want to start the process by MessageStartEvent, so I will need the ServiceTask to create an entity that gets persisted into db. Can it be achieved in some way? I've tried the Activiti in Action book as well as the Activiti documentation, but I can't seem to find such example.

Example Java ServiceTask Delegate:

public class ServiceTaskNewOrder implements JavaDelegate{

   @Override
   public void execute(DelegateExecution execution) throws Exception {
                //this works ok, but how can I create here an entity that will be stored into my second not-activiti database?
      Order o = (Order) execution.getVariable("order");
      
   }
}


If it can't be done with this configuration then I'm open for any advice how to do it in xml.

Please forgive me if it is somewhere in documentation or the forums.

Many thanks for any help. I've started with activiti just few days ago, but I can't resolve this step :)

Outcomes