cancel
Showing results for 
Search instead for 
Did you mean: 

JPA and multiple EntityManager

korenaga
Champ in-the-making
Champ in-the-making
As described in the following chapter in the Activiti user guide (http://activiti.org/userguide/#N1200A) it is possible to use JPA in combination with Activiti. This should then in turn use JPA to retrieve stored process variables.  This is exactly what we need. But the problem is is that the project I'm working on at this moment uses multiple entity manager factories during the process. I can't find a way to do this with Activiti. Am I missing something or is it not possible?
3 REPLIES 3

frederikherema1
Star Contributor
Star Contributor
The current implementations uses a single EntityManagerFactory to obtain an EntityManager from when dealing with JPAVariables. If you want to use multiple EntityManagerFactories, you'll have to implement your own "MyJPAVariableType". This way, without altering activiti source-code, you can plug in the behavior you want.

A couple of pointers:

  • Start with looking into and extending org.activiti.engine.impl.variable.JPAEntityVariableType

  • Your variable-type should, on save and load, use a "custom" mechanism to locate the right entity-manager for the right entity-type, instead of using the global org.activiti.engine.impl.variable.EntityManagerSession

  • Implement a similar "Session" as org.activiti.engine.impl.variable.EntityManagerSession, which allows storing multiple entity-managers for the duration of a single API-call in activiti.

korenaga
Champ in-the-making
Champ in-the-making
Thanks!

So just for clarification (and possible future reference);
  • I need to create a CustomJPAEntityVariable (extending JPAEntityVariable) and add it to the customPreVariable List in the ProcessEngineConfigurationImpl class

  • Then I need to create a CustomEntityManagerSessionFactory and add it to the customSessionFactories List in the ProcessEngineConfigurationImpl class

  • The CustomEntityManagerSessionFactory class will return an instance of the CustomEntityManagerSession when the openSession() method is called

  • The CustomEntityManagerSession will make it possible to have multiple EntityManagers
That's about it in rough lines I think right?

frederikherema1
Star Contributor
Star Contributor
Yes, that's exactly how to do it…