AnsweredAssumed Answered

Java service task, JavaDelegate JPA

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

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="" xmlns:xsi="" xsi:schemaLocation="">
      <persistence-unit name="DAO">
      …my classes…
            <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="" value="update" />     

I configure my process Engine like this:

ProcessEngineConfigurationImpl pEC = (ProcessEngineConfigurationImpl) ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
      processEngine = pEC.buildProcessEngine();

I get entityManager like this:

EntityManagerSessionFactory entityManagerSessionFactory = (EntityManagerSessionFactory) pEC
      entityManagerFactory = entityManagerSessionFactory.getEntityManagerFactory();

And I can persist the entity:

EntityManager manager = entityManagerFactory.createEntityManager();
       Order newOrder=new Order();      
…doing stuff here order related….
      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{

   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 :)