<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: JPA process variables not fully supported? in Alfresco Archive</title>
    <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113341#M79806</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thank u for your quick response. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If i do not tell the session to flush then why it does so? In fact it flushes even after the entity is read via JpaEntityMapping… And when the CommandContext is closed all session managers are considered and a flush is executed on them. (CommandContext.close() -&amp;gt; closeSessions() -&amp;gt; EntityManagerSessionImpl.flush()) &lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt;&amp;nbsp; public void flush() {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (entityManager != null &amp;amp;&amp;amp; (!handleTransactions || isTransactionActive()) ) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entityManager.flush();&lt;BR /&gt;&lt;/CODE&gt;&lt;SPAN&gt;The reason why there is no EntityManager available when the task is completed is that no one is created using the then active EntityManagerSession. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It just tries to use the EntityManager which is held in the EntityManagerSessionImpl instance. But, as u can see in the snippet posted above, getEntityManager() is not called in flush(). The only one which is calling getEntityManager() on EntityManagerSession is JpaEntityMappings when the entity is read when the user task is started (through JPAEntityVariableType.getValue() which calls JPAEntityMappings.getJPAEntity(), and findEntity()). But that session is discarded as mentioned earlier together with its cached EntityManager. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Furthermore i think a em.merge() and em.persist() would be needed somewhere before the flush when the entity is stored to the JPAEntityVariableType? &lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt;public void setValue(Object value, ValueFields valueFields) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityManagerSession entityManagerSession = Context &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .getCommandContext()&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .getSession(EntityManagerSession.class);&lt;BR /&gt;&lt;/CODE&gt;&lt;SPAN&gt;Why not get an EntityManager there and perform an em.merge(value)? Then the EntityManagerSession would have an EM for the flush() operation and the JPA entity would be attached to the persistence context? Or do we need to implement the merge and persist logic ourself, and if so, how can we align the containers EntityManager with the one Activiti will use? Activiti will, if it would do it, get an EntityManager using the configured EntityManagerFactory. But for sure it will not get the same EntityManger which is injected to lets say service tasks which resolve to EJBs…&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And if i read the code, what ever i would configure with jpaHandleTransaction or jpaCloseEntityManager, it would not make any difference in this case, as the EntityManager is null in my case when the task is completed.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried to understand why my rather simple use case which is promoted in the manual and here on the forum and some blog postings is not working, and during some debugging sessions it seems that the code does not support this, at least my, use case. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For a quick try i just added these four lines of code to the JPAEntityVariableType.setValue() method (from 5.10-SNAPSHOT):&lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt;&amp;nbsp; public void setValue(Object value, ValueFields valueFields) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityManagerSession entityManagerSession = Context &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .getCommandContext()&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .getSession(EntityManagerSession.class);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (entityManagerSession == null) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ActivitiException("Cannot set JPA variable: " + EntityManagerSession.class + " not configured");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // get EM and merge entity to be flushed afterwards&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityManager entityManager = entityManagerSession.getEntityManager();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (entityManager != null) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entityManager.merge(value);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Before we set the value we must flush all pending changes from the entitymanager&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // If we don't do this, in some cases the primary key will not yet be set in the object&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // which will cause exceptions down the road.&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entityManagerSession.flush();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;/CODE&gt;&lt;SPAN&gt;it works nicely for me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, what do think, are these two lines of code valid there? Or are there other side effects expected?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 01 Aug 2012 20:46:34 GMT</pubDate>
    <dc:creator>chris_joelly</dc:creator>
    <dc:date>2012-08-01T20:46:34Z</dc:date>
    <item>
      <title>JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113335#M79800</link>
      <description>Hello,i just figured out that JPA entities are defined as not cachable in JPAEntityVariableType.I have the following use case where this is a real problem:i use JSF/activiti-cdi, in the JSF based user task views i use things like:&amp;lt;customer:detail customer="#{processVariables['order'].customer}" /</description>
      <pubDate>Sat, 28 Jul 2012 10:47:43 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113335#M79800</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-07-28T10:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113336#M79801</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;After having the idea that the JPA process variable should be persisted when the user task is completed using the BusinessProcess.completeTask() method i tried to find out why it is not persisted. If it then is re-read from the database it would solve my initial problem. Maybe this is not a good thing from the performance perspective, but from the transactional behavior it is a nice thing for sure, as the user task is completed and might be persisted the process variables should be in sync as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Maybe i am wrong, but when the process variable is read when the JSF form is opened using #{processVariables['order'] an EntityManagerSession is created and when the JPA variable is read it calls the EntityManagerFactory to obtain an EntityManager, which is then cached in that EMS. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But when the task is completed another EntityManagerSession is created and it gets the EntityManagerFactory from the application server again. But when the flush() method is hit the EntityManager is not initialized, the EMF is never asked to hand out an EM. So, the flush is not performed and the process variable is not persisted.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What am i doing wrong? Is this use case simply not considered (JPA variable with a user task followed by a service task)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Stack Trace when loading the JPA variable:&lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt; EntityManagerSessionImpl.getEntityManager() line: 84 &lt;BR /&gt; JPAEntityMappings.findEntity(Class&amp;lt;?&amp;gt;, Object) line: 123 &lt;BR /&gt; JPAEntityMappings.getJPAEntity(String, String) line: 119 &lt;BR /&gt; JPAEntityVariableType.getValue(ValueFields) line: 77 &lt;BR /&gt; VariableInstanceEntity.getValue() line: 158 &lt;BR /&gt; ExecutionEntity(VariableScopeImpl).getVariable(String) line: 93 &lt;BR /&gt; GetExecutionVariableCmd.execute(CommandContext) line: 60 &lt;BR /&gt; CommandExecutorImpl.execute(Command&amp;lt;T&amp;gt;) line: 24 &lt;BR /&gt; CommandContextInterceptor.execute(Command&amp;lt;T&amp;gt;) line: 42 &lt;BR /&gt; JtaTransactionInterceptor.execute(Command&amp;lt;T&amp;gt;) line: 59 &lt;BR /&gt; LogInterceptor.execute(Command&amp;lt;T&amp;gt;) line: 33 &lt;BR /&gt; RuntimeServiceImpl.getVariable(String, String) line: 102 &lt;BR /&gt; UiMediatedBusinessProcessBean(BusinessProcess).getVariable(String) line: 303 &lt;BR /&gt; ProcessVariableMap.get(Object) line: 43 &lt;BR /&gt; MapELResolver.getValue(ELContext, Object, Object) line: 196 &lt;BR /&gt; DemuxCompositeELResolver._getValue(int, ELResolver[], ELContext, Object, Object) line: 176 &lt;BR /&gt; DemuxCompositeELResolver.getValue(ELContext, Object, Object) line: 203 &lt;BR /&gt; AstValue.getValue(EvaluationContext) line: 169 &lt;BR /&gt; ValueExpressionImpl.getValue(ELContext) line: 189 &lt;BR /&gt; WeldValueExpression.getValue(ELContext) line: 50 &lt;BR /&gt; TagValueExpression.getValue(ELContext) line: 109 &lt;BR /&gt; UIComponentBase$AttributesMap.get(Object) line: 2362 &lt;BR /&gt; CompositeComponentAttributesELResolver$ExpressionEvalMap.get(Object) line: 345 &lt;BR /&gt; MapELResolver.getValue(ELContext, Object, Object) line: 196 &lt;BR /&gt; DemuxCompositeELResolver._getValue(int, ELResolver[], ELContext, Object, Object) line: 176 &lt;BR /&gt; DemuxCompositeELResolver.getValue(ELContext, Object, Object) line: 203 &lt;BR /&gt; AstValue.getValue(EvaluationContext) line: 169 &lt;BR /&gt; ValueExpressionImpl.getValue(ELContext) line: 189 &lt;BR /&gt; WeldValueExpression.getValue(ELContext) line: 50 &lt;BR /&gt; ContextualCompositeValueExpression.getValue(ELContext) line: 156 &lt;BR /&gt; TagValueExpression.getValue(ELContext) line: 109 &lt;BR /&gt; ComponentStateHelper.eval(Serializable, Object) line: 194 &lt;BR /&gt; ComponentStateHelper.eval(Serializable) line: 182 &lt;BR /&gt; HtmlOutputText(UIOutput).getValue() line: 169 &lt;BR /&gt; TextRenderer(HtmlBasicInputRenderer).getValue(UIComponent) line: 205 &lt;BR /&gt; TextRenderer(HtmlBasicRenderer).getCurrentValue(FacesContext, UIComponent) line: 355 &lt;BR /&gt; TextRenderer(HtmlBasicRenderer).encodeEnd(FacesContext, UIComponent) line: 164 &lt;BR /&gt; HtmlOutputText(UIComponentBase).encodeEnd(FacesContext) line: 875 &lt;BR /&gt; GridRenderer(HtmlBasicRenderer).encodeRecursive(FacesContext, UIComponent) line: 312 &lt;BR /&gt; GridRenderer.renderRow(FacesContext, UIComponent, UIComponent, ResponseWriter) line: 185 &lt;BR /&gt; GridRenderer.encodeChildren(FacesContext, UIComponent) line: 129 &lt;BR /&gt; HtmlPanelGrid(UIComponentBase).encodeChildren(FacesContext) line: 845 &lt;BR /&gt; GridRenderer(HtmlBasicRenderer).encodeRecursive(FacesContext, UIComponent) line: 304 &lt;BR /&gt; GridRenderer.renderRow(FacesContext, UIComponent, UIComponent, ResponseWriter) line: 185 &lt;BR /&gt; GridRenderer.encodeChildren(FacesContext, UIComponent) line: 129 &lt;BR /&gt; HtmlPanelGrid(UIComponentBase).encodeChildren(FacesContext) line: 845 &lt;BR /&gt; GroupRenderer(HtmlBasicRenderer).encodeRecursive(FacesContext, UIComponent) line: 304 &lt;BR /&gt; GroupRenderer.encodeChildren(FacesContext, UIComponent) line: 105 &lt;BR /&gt; UIPanel(UIComponentBase).encodeChildren(FacesContext) line: 845 &lt;BR /&gt; UIPanel(UIComponent).encodeAll(FacesContext) line: 1779 &lt;BR /&gt; CompositeRenderer.encodeChildren(FacesContext, UIComponent) line: 78 &lt;BR /&gt; UINamingContainer(UIComponentBase).encodeChildren(FacesContext) line: 845 &lt;BR /&gt; UINamingContainer(UIComponent).encodeAll(FacesContext) line: 1779 &lt;BR /&gt; FormRenderer(Renderer).encodeChildren(FacesContext, UIComponent) line: 168 &lt;BR /&gt; HtmlForm(UIComponentBase).encodeChildren(FacesContext) line: 845 &lt;BR /&gt; OutputPanelRenderer(CoreRenderer).renderChild(FacesContext, UIComponent) line: 55 &lt;BR /&gt; OutputPanelRenderer(CoreRenderer).renderChildren(FacesContext, UIComponent) line: 43 &lt;BR /&gt; OutputPanelRenderer.encodeEnd(FacesContext, UIComponent) line: 44 &lt;BR /&gt; OutputPanel(UIComponentBase).encodeEnd(FacesContext) line: 875 &lt;BR /&gt; GroupRenderer(HtmlBasicRenderer).encodeRecursive(FacesContext, UIComponent) line: 312 &lt;BR /&gt; GroupRenderer.encodeChildren(FacesContext, UIComponent) line: 105 &lt;BR /&gt; HtmlPanelGroup(UIComponentBase).encodeChildren(FacesContext) line: 845 &lt;BR /&gt; GridRenderer(HtmlBasicRenderer).encodeRecursive(FacesContext, UIComponent) line: 304 &lt;BR /&gt; GridRenderer.renderRow(FacesContext, UIComponent, UIComponent, ResponseWriter) line: 185 &lt;BR /&gt; GridRenderer.encodeChildren(FacesContext, UIComponent) line: 129 &lt;BR /&gt; HtmlPanelGrid(UIComponentBase).encodeChildren(FacesContext) line: 845 &lt;BR /&gt; HtmlPanelGrid(UIComponent).encodeAll(FacesContext) line: 1779 &lt;BR /&gt; UIOutput(UIComponent).encodeAll(FacesContext) line: 1782 &lt;BR /&gt; UIViewRoot(UIComponent).encodeAll(FacesContext) line: 1782 &lt;BR /&gt;&lt;/CODE&gt;&lt;BR /&gt;&lt;SPAN&gt;Stack Trace when completing the user task and flushing the JPA variable:&lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt; EntityManagerSessionImpl.flush() line: 63 &lt;BR /&gt; JPAEntityVariableType.setValue(Object, ValueFields) line: 61 &lt;BR /&gt; VariableInstanceEntity.setValue(Object) line: 164 &lt;BR /&gt; ExecutionEntity(VariableScopeImpl).setVariableInstanceValue(Object, VariableInstanceEntity) line: 196 &lt;BR /&gt; ExecutionEntity(VariableScopeImpl).setVariableLocal(String, Object) line: 189 &lt;BR /&gt; ExecutionEntity(VariableScopeImpl).setVariable(String, Object) line: 167 &lt;BR /&gt; ExecutionEntity(VariableScopeImpl).setVariables(Map&amp;lt;String,Object&amp;gt;) line: 264 &lt;BR /&gt; TaskEntity.setExecutionVariables(Map&amp;lt;String,Object&amp;gt;) line: 360 &lt;BR /&gt; CompleteTaskCmd.execute(CommandContext) line: 54 &lt;BR /&gt; CompleteTaskCmd.execute(CommandContext) line: 28 &lt;BR /&gt; CommandExecutorImpl.execute(Command&amp;lt;T&amp;gt;) line: 24 &lt;BR /&gt; CommandContextInterceptor.execute(Command&amp;lt;T&amp;gt;) line: 42 &lt;BR /&gt; JtaTransactionInterceptor.execute(Command&amp;lt;T&amp;gt;) line: 59 &lt;BR /&gt; LogInterceptor.execute(Command&amp;lt;T&amp;gt;) line: 33 &lt;BR /&gt; TaskServiceImpl.complete(String, Map&amp;lt;String,Object&amp;gt;) line: 148 &lt;BR /&gt; UiMediatedBusinessProcessBean(BusinessProcess).completeTask() line: 263 &lt;BR /&gt; UiMediatedBusinessProcessBean.completeTask() line: 53 &lt;BR /&gt; UiMediatedBusinessProcessBean(BusinessProcess).completeTask(boolean) line: 274 &lt;BR /&gt; NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] &lt;BR /&gt; NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39 &lt;BR /&gt; DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25 &lt;BR /&gt; Method.invoke(Object, Object…) line: 597 &lt;BR /&gt; AstValue.invoke(EvaluationContext, Class[], Object[]) line: 262 &lt;BR /&gt; MethodExpressionImpl.invoke(ELContext, Object[]) line: 278 &lt;BR /&gt; WeldMethodExpression(ForwardingMethodExpression).invoke(ELContext, Object[]) line: 39 &lt;BR /&gt; WeldMethodExpression.invoke(ELContext, Object[]) line: 50 &lt;BR /&gt; TagMethodExpression.invoke(ELContext, Object[]) line: 105 &lt;BR /&gt; MethodBindingMethodExpressionAdapter.invoke(FacesContext, Object[]) line: 88 &lt;BR /&gt; ActionListenerImpl.processAction(ActionEvent) line: 102 &lt;BR /&gt; HtmlCommandButton(UICommand).broadcast(FacesEvent) line: 315 &lt;BR /&gt;&lt;/CODE&gt;&lt;BR /&gt;&lt;SPAN&gt;So, simply spoken, with this behavior and the fact that JPA variables are not cachable we will not be able to use JPA variables in a way like i do?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Using them via CDI on JSF with user tasks and service tasks?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 28 Jul 2012 18:10:09 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113336#M79801</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-07-28T18:10:09Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113337#M79802</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Can you show how you configure your engine (the XML), especially the part where the JPA-stuff is configured&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jul 2012 10:30:40 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113337#M79802</guid>
      <dc:creator>frederikherema1</dc:creator>
      <dc:date>2012-07-31T10:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113338#M79803</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;here is the cfg:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt; &amp;lt;jee:jndi-lookup id="dataSource" jndi-name="java:/datasources/appDS" /&amp;gt;&lt;BR /&gt; &lt;BR /&gt; &amp;lt;!– use transaction manager from the application server –&amp;gt;&lt;BR /&gt; &amp;lt;jee:jndi-lookup id="transactionManager" jndi-name="java:jboss/TransactionManager" /&amp;gt;&lt;BR /&gt; &amp;lt;jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/appEMF" resource-ref="false" /&amp;gt;&lt;BR /&gt;&lt;BR /&gt; &amp;lt;!– integrate Activiti with CDI and JTA –&amp;gt;&lt;BR /&gt; &amp;lt;bean id="processEngineConfiguration"&lt;BR /&gt;&amp;nbsp; class="org.activiti.cdi.CdiJtaProcessEngineConfiguration"&amp;gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;!– config for JTA managed persistence –&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="dataSource" ref="dataSource" /&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="transactionManager" ref="transactionManager"/&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="transactionsExternallyManaged" value="true" /&amp;gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; &amp;lt;property name="jpaEntityManagerFactory" ref="entityManagerFactory" /&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="jpaHandleTransaction" value="false" /&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="jpaCloseEntityManager" value="false" /&amp;gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;!– upgrade Activiti model if needed –&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="databaseSchemaUpdate" value="true" /&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="databaseType" value="mysql" /&amp;gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;!– enable timers –&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="jobExecutorActivate" value="true" /&amp;gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; &amp;lt;!– log full details to the history tables –&amp;gt;&lt;BR /&gt;&amp;nbsp; &amp;lt;property name="history" value="full" /&amp;gt;&lt;BR /&gt; &amp;lt;/bean&amp;gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jul 2012 19:38:14 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113338#M79803</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-07-31T19:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113339#M79804</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;During debugging the whole thing i discovered that the GetExecutionVariableCmd is executed when i open the user task with a JSF form and i access the JPA entity process variable. There a EntityManagerSession is created from the session factory and put into the command context. In that session an entity manager is used to load the entity. After the GetExecutionVariableCmd is finished with its execution the session is flushed and finally closed. In fact the whole command context is discarded.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When i then modify the entity through the JSF form and execute a completeTask which ends in the CompleteTaskCommand, a new command context is build for it. There a new EntityManagerSession is created using the session factory and of course there is no entity manager available. It would only be available when it would have been stored there using the call to getEntityManager(), which unfortunately only happens when JPAEntityMappings.findEntity() would have been called. So, i added this call to EntityManagerSessionImpl.flush(), but it did not help. Well, this for sure has its reason that with that entity manager no entity is associated… i think a merge would be needed so the entity gets flushed using that newly created entity manager.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So… finally i think regardless what i set in the activiti.cfg.xml, the whole thing with JPA entities does not work when a new EntityManagerSession is created for every command…&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jul 2012 21:25:21 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113339#M79804</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-07-31T21:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113340#M79805</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;CODE&gt;&lt;BR /&gt;&amp;lt;property name="jpaEntityManagerFactory" ref="entityManagerFactory" /&amp;gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="jpaHandleTransaction" value="false" /&amp;gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="jpaCloseEntityManager" value="false" /&amp;gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;BR /&gt;&lt;SPAN&gt;A lot depends on your environment? You tell the session NOT to flush, nor close the entityManager. You're correct about the command context being discarded every time an API-call is done, this is intended behavior. There is no easy way of "keeping the same EntityManager" in between API-calls, because activiti is not aware of this off course (can take a long time between API-call, or never called again etc).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Why is there no EntityManager available when the task is completed? The EntityManagerFactory is responsible for creating a new EntityManager when requested. This should have access to the previously stored JPA-entities, no?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2012 06:05:47 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113340#M79805</guid>
      <dc:creator>frederikherema1</dc:creator>
      <dc:date>2012-08-01T06:05:47Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113341#M79806</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thank u for your quick response. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If i do not tell the session to flush then why it does so? In fact it flushes even after the entity is read via JpaEntityMapping… And when the CommandContext is closed all session managers are considered and a flush is executed on them. (CommandContext.close() -&amp;gt; closeSessions() -&amp;gt; EntityManagerSessionImpl.flush()) &lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt;&amp;nbsp; public void flush() {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (entityManager != null &amp;amp;&amp;amp; (!handleTransactions || isTransactionActive()) ) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entityManager.flush();&lt;BR /&gt;&lt;/CODE&gt;&lt;SPAN&gt;The reason why there is no EntityManager available when the task is completed is that no one is created using the then active EntityManagerSession. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It just tries to use the EntityManager which is held in the EntityManagerSessionImpl instance. But, as u can see in the snippet posted above, getEntityManager() is not called in flush(). The only one which is calling getEntityManager() on EntityManagerSession is JpaEntityMappings when the entity is read when the user task is started (through JPAEntityVariableType.getValue() which calls JPAEntityMappings.getJPAEntity(), and findEntity()). But that session is discarded as mentioned earlier together with its cached EntityManager. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Furthermore i think a em.merge() and em.persist() would be needed somewhere before the flush when the entity is stored to the JPAEntityVariableType? &lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt;public void setValue(Object value, ValueFields valueFields) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityManagerSession entityManagerSession = Context &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .getCommandContext()&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .getSession(EntityManagerSession.class);&lt;BR /&gt;&lt;/CODE&gt;&lt;SPAN&gt;Why not get an EntityManager there and perform an em.merge(value)? Then the EntityManagerSession would have an EM for the flush() operation and the JPA entity would be attached to the persistence context? Or do we need to implement the merge and persist logic ourself, and if so, how can we align the containers EntityManager with the one Activiti will use? Activiti will, if it would do it, get an EntityManager using the configured EntityManagerFactory. But for sure it will not get the same EntityManger which is injected to lets say service tasks which resolve to EJBs…&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And if i read the code, what ever i would configure with jpaHandleTransaction or jpaCloseEntityManager, it would not make any difference in this case, as the EntityManager is null in my case when the task is completed.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried to understand why my rather simple use case which is promoted in the manual and here on the forum and some blog postings is not working, and during some debugging sessions it seems that the code does not support this, at least my, use case. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For a quick try i just added these four lines of code to the JPAEntityVariableType.setValue() method (from 5.10-SNAPSHOT):&lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt;&amp;nbsp; public void setValue(Object value, ValueFields valueFields) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityManagerSession entityManagerSession = Context &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .getCommandContext()&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .getSession(EntityManagerSession.class);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (entityManagerSession == null) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ActivitiException("Cannot set JPA variable: " + EntityManagerSession.class + " not configured");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // get EM and merge entity to be flushed afterwards&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityManager entityManager = entityManagerSession.getEntityManager();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (entityManager != null) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entityManager.merge(value);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Before we set the value we must flush all pending changes from the entitymanager&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // If we don't do this, in some cases the primary key will not yet be set in the object&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // which will cause exceptions down the road.&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entityManagerSession.flush();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;/CODE&gt;&lt;SPAN&gt;it works nicely for me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, what do think, are these two lines of code valid there? Or are there other side effects expected?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2012 20:46:34 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113341#M79806</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-08-01T20:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113342#M79807</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your approach seems valid, up to the point where you "flush" the entity-manager on each "set" of a variable. The entity-manager should only be flushed at the end of the command-context-chain (if all goes well).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So instead of getting the entity-manager directly in the setValue(), rather you should initialize the JPAEntityManagerSession the way the reading of JPA-variables does. This way, IF a variable is set, the entity manager is initialized in the session and will be not-null when session is flushed, resulting in the desired behavior.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could you try this approach and perhaps attach the patch, so we can introduce this in the acidity code-base?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2012 06:39:32 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113342#M79807</guid>
      <dc:creator>frederikherema1</dc:creator>
      <dc:date>2012-08-02T06:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113343#M79808</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hm…&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;i dont understand what u mean with "up to the point where you "flush" the entity-manager on each "set" of a variable".&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the usual behavior of the code on 5.10-SNAPSHOT, i just added the following four lines, as stated in my previous post:&lt;/SPAN&gt;&lt;BR /&gt;&lt;CODE&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // get EM and merge entity to be flushed afterwards&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityManager entityManager = entityManagerSession.getEntityManager();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (entityManager != null) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entityManager.merge(value);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;/CODE&gt;&lt;SPAN&gt;AFAIK a em.merge() does not perform a flush?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The call to entityManagerSession.flush() was already there …&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But i will investigate on your proposal…&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2012 11:33:25 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113343#M79808</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-08-02T11:33:25Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113344#M79809</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Sorry, I was mistaken. You're right about the fact that the flush is already there…&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;No I truly get what is going on and what your use case is. The activiti-variable is only responsible for actually storing the reference to the entity, not handling potential changes in the entity itself. Therefor, the entity isn't merged with the entity-manager, because we're only interested in the ID and classname. In case of a jpa-entity created in the SAME entity-manager, the flush() before getting the ID is done to ensure all JPA-entities have an ID set.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So in your case, where you compete the task with a JPA-entity, activiti assumes you have already saved (has an ID) OR will be assigned an ID if the same entity-manager has been used. You should, before passing on the JPA-entity to activiti, merge it yourself. This is not a responsibility of activiti imho.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2012 11:51:57 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113344#M79809</guid>
      <dc:creator>frederikherema1</dc:creator>
      <dc:date>2012-08-02T11:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113345#M79810</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;ok.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;well, this conclusion matches with my conclusion after i studied the code during my debugging sessions. I was heavily missleaded by the user guide and some blog postings which say that Activiti supports JPA entities as process variables, which it actually does when it comes to reading entities from the database. When it comes to storing them, and for sure people want to modify them during user tasks and service tasks, then Activiti support is not there. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Especially the blog post by Bernd Rücker (&lt;/SPAN&gt;&lt;A href="http://www.bpm-guide.de/2012/04/04/pageflow-vs-process-flow-and-ui-mediator-pattern/" rel="nofollow noopener noreferrer"&gt;http://www.bpm-guide.de/2012/04/04/pageflow-vs-process-flow-and-ui-mediator-pattern/&lt;/A&gt;&lt;SPAN&gt;) gives the impression that using activiti-cdi together with JSF and other JEE technologies makes such use cases possible. Only JPA variables are not covered.. Because they dont work out of the box in such an environment when they are modified through JSF and wants to get persisted when BusinessProcess.completeTask() is executed from an JSF command … &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2012 14:19:52 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113345#M79810</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-08-02T14:19:52Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113346#M79811</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, that's true, when working in such environments (and activiti participates in such transitions/contexts) the updates to the variables themselves are reflected as where activiti out of the box, only uses the id's. I didn't grasp the initial problem you had, so tok a while to see &lt;img id="smileywink" class="emoticon emoticon-smileywink" src="https://connect.hyland.com/i/smilies/16x16_smiley-wink.png" alt="Smiley Wink" title="Smiley Wink" /&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2012 14:28:25 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113346#M79811</guid>
      <dc:creator>frederikherema1</dc:creator>
      <dc:date>2012-08-02T14:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113347#M79812</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I want to come back to the following statements:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Your approach seems valid, up to the point where you "flush" the entity-manager on each "set" of a variable. The entity-manager should only be flushed at the end of the command-context-chain (if all goes well).&lt;/BLOCKQUOTE&gt;&lt;SPAN&gt;What is the command-context-chain? When i complete a user task, this starts a command-context-chain till execution arrives at the next wait state? Or from the start process to some wait state? Well, with the ensure-to-get-the-enities-id-flush this strategy already seems broken. From a performance perspective this might have an influence if many JPA entities are used as process variables and a flush is executed after each setValue. But if something goes bad a roll back is executed and the state of the process will still be valid, in terms of the execution and the data are synchronized?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;So instead of getting the entity-manager directly in the setValue(), rather you should initialize the JPAEntityManagerSession the way the reading of JPA-variables does. This way, IF a variable is set, the entity manager is initialized in the session and will be not-null when session is flushed, resulting in the desired behavior.&lt;/BLOCKQUOTE&gt;&lt;SPAN&gt;With JPAEntityManagerSession u mean the EntityManagerSessionImpl? if i got u right this is what i did when adding the em.merge… What about removing the entityManagerSession.flush() from the JPAEntityVariableType.setValue() call? This is only needed if JPA entities are created within Activiti tasks which dont have their identity set? In my case this would never be the case because the processes will always work on already existing entities. But as it has only a minor performance impact i think its not a big deal?&lt;/SPAN&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Could you try this approach and perhaps attach the patch, so we can introduce this in the acidity code-base?&lt;/BLOCKQUOTE&gt;&lt;SPAN&gt;Well, that 4 lines of codes … if Activiti is designed the way that it does not touch JPA entities it might introduce side effects on other users which expect Activiti to not change their JPA entities? Maybe it is possible to introduce it with another jpaXXX config parameter for activiti.cfg.xml which defaults to the actual behavior?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 Aug 2012 17:31:17 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113347#M79812</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-08-04T17:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: JPA process variables not fully supported?</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113348#M79813</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Today i tried to create an JPA entity from within an service task which is executed via an process which is triggered by a start timer event and set this newly created entity as process variable. The entity is just instantiated with new and no PersistenceContext was involved. only setters are called to set its member variables.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I did not expect that it would work, but the things i observed was somehow strange &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The summary is: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the entity was persisted two times, with two different ids, but only the second one was stored as process variable. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now the long story &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think the 'problem' is in the method VariableScopeImpl.createVariableLocal() where the entity is stored 'twice' to the variableInstance, first with the VariableInstanceEntity.createAndInsert() call where the entity is modified during the flush and merge and then with the setVariableInstanceValue(value, variableInstance), which still use the unmodified entity because the merge and flush operations are not propagated back to the original entity.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I just added a value = variableInstance.getValue() just before the setVariableInstanceValue(value, variableInstance) and re-read the entity from the persistence context. And now the second entity is not created and everything looks fine. At least for my use cases.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, finally i replaced the entityManagerSession.flush() with an em.merge() in JPAEntityVariableType.setValue() and added the above mentioned line to&amp;nbsp; VariableScopeImpl.createVariableLocal() to make the persistence of existing and new JPA entities work with Activiti.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What do u think? Would this introduce side effects?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One downside i could already see when i run the Activiti unit tests:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is one of the JPA entities used in the JPA test cases, and how they look before and after the merge:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;before:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;org.activiti.standalone.jpa.FieldAccessJPAEntity@4260ab&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;after:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;org.apache.openjpa.enhance.org$activiti$standalone$jpa$FieldAccessJPAEntity$pcsubclass@df88d2&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and because the @Entity annotation is not declared @Inherited it seems that the OpenJPA enhancement removes the Entity annotation of the JPA entities when they are merged to a persistence contet…&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, the detection if an object is an JPA entity breaks now when using OpenJPA as JPA provider when a merge is performed. It seems that the unit tests never run with entities which belonged to an persistence context?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;some related stuff:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://greensopinion.blogspot.co.at/2009/08/how-i-lost-my-annotations-jpa-hibernate.html" rel="nofollow noopener noreferrer"&gt;http://greensopinion.blogspot.co.at/2009/08/how-i-lost-my-annotations-jpa-hibernate.html&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://struberg.wordpress.com/2012/01/08/jpa-enhancement-done-right/" rel="nofollow noopener noreferrer"&gt;http://struberg.wordpress.com/2012/01/08/jpa-enhancement-done-right/&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 05 Aug 2012 16:35:14 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/jpa-process-variables-not-fully-supported/m-p/113348#M79813</guid>
      <dc:creator>chris_joelly</dc:creator>
      <dc:date>2012-08-05T16:35:14Z</dc:date>
    </item>
  </channel>
</rss>

