cancel
Showing results for 
Search instead for 
Did you mean: 

Java service task, JavaDelegate JPA

nexowski
Champ in-the-making
Champ in-the-making
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 Smiley Happy 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 Smiley Happy
2 REPLIES 2

jbarrez
Star Contributor
Star Contributor
Can't you simply do the same in the service task: get the entity manager and do the jpa magic? Or am I missing something here?

nexowski
Champ in-the-making
Champ in-the-making
Ok I've managed with it. Indeed created own classes to handle the database operations (I wanted one and the same EntityManager). Working now ith REST api and found another problem which I'll post in a while. This topic can be deleted I think it does not give any information.