<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:arial, helvetica, sans-serif;font-size:12pt"><DIV>I'm not sure this should be comitted to the trunk.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Users&nbsp;can't use this code as is, because:</DIV>
<DIV>&nbsp;</DIV>
<DIV>1. Manual/Automatic flush </DIV>
<DIV>&nbsp;&nbsp; - they'd need to implement flush() and persist() if they have manual flush set up.</DIV>
<DIV>2. HibUtil </DIV>
<DIV>&nbsp;&nbsp; - is a system specific implementation used to manage SessionFactory and Sessions</DIV>
<DIV>&nbsp;&nbsp; - systems using Hibernate will already have some version of this they will need to leverage</DIV>
<DIV>&nbsp;</DIV>
<DIV>so this is more of a template.&nbsp; </DIV>
<DIV>&nbsp;</DIV>
<DIV>In fact, it won't compile without a HibUtil, so that's another problem for adding it to the trunk...</DIV>
<DIV>&nbsp;</DIV>
<DIV>A test case in examples would let us create a simple HibUtil and hibernate.cfg.xml.</DIV>
<DIV><BR>The test case could create a H2 DB and the tables needed...</DIV>
<DIV>&nbsp;</DIV>
<DIV style="FONT-FAMILY: arial, helvetica, sans-serif; FONT-SIZE: 12pt"><BR>
<DIV style="FONT-FAMILY: times new roman, new york, times, serif; FONT-SIZE: 12pt"><FONT size=2 face=Tahoma>
<HR SIZE=1>
<B><SPAN style="FONT-WEIGHT: bold">From:</SPAN></B> Mauricio Salatino &lt;salaboy@gmail.com&gt;<BR><B><SPAN style="FONT-WEIGHT: bold">To:</SPAN></B> Rules Users List &lt;rules-users@lists.jboss.org&gt;<BR><B><SPAN style="FONT-WEIGHT: bold">Sent:</SPAN></B> Fri, October 23, 2009 4:18:16 PM<BR><B><SPAN style="FONT-WEIGHT: bold">Subject:</SPAN></B> Re: [rules-users] [droolsflow] - one way to use Hibernate mapping with JPAVariablePersister<BR></FONT><BR>nice.. do you mind if I commit it to the trunk?<BR>can you create some unit testing too?<BR><BR><BR>
<DIV class=gmail_quote>On Fri, Oct 23, 2009 at 3:27 PM, Bill Tarr <SPAN dir=ltr>&lt;<A href="mailto:javatestcase@yahoo.com" rel=nofollow target=_blank ymailto="mailto:javatestcase@yahoo.com">javatestcase@yahoo.com</A>&gt;</SPAN> wrote:<BR>
<BLOCKQUOTE style="BORDER-LEFT: rgb(204,204,204) 1px solid; MARGIN: 0pt 0pt 0pt 0.8ex; PADDING-LEFT: 1ex" class=gmail_quote>Here is an alternate version of JPAVariablePersister for people who are already using Hibernate xml&nbsp;mapping files.&nbsp;<BR><BR>This implementation has some downsides, but it seems to be working for now.<BR><BR>I am going to keep using JPA to manage all the Drools Flow tables.<BR><BR>My domain object persistence will be managed by Hibernate Session.&nbsp;<BR><BR>We are also currently using automatic flush (so no code to persist domain objects in this class)<BR><BR>Obviously you need to use your own version of HibUtil.<BR><BR>import java.util.logging.Level;<BR>import java.util.logging.Logger;<BR>import org.drools.persistence.processinstance.variabletypes.JPAPersistedVariable;<BR>import org.drools.persistence.processinstance.variabletypes.VariableInstanceInfo;<BR>import
 org.drools.persistence.processinstance.persisters.VariablePersister;<BR>import org.drools.runtime.Environment;<BR>import org.hibernate.Session;<BR>import HibUtil;<BR>/**<BR>&nbsp;* Variation on Drools JPA Variable Persister.<BR>&nbsp;* org.drools.persistence.processinstance.persisters.JPAVariablePersister<BR>&nbsp;* for using variables persisted by Hibernate, while still using JPA to<BR>&nbsp;* persist DroolsFlow data.<BR>&nbsp;*<BR>&nbsp;* @author &lt;a href="mailto:<A href="mailto:kris_verlaenen@hotmail.com" rel=nofollow target=_blank ymailto="mailto:kris_verlaenen@hotmail.com">kris_verlaenen@hotmail.com</A>"&gt;Kris Verlaenen&lt;/a&gt;<BR>&nbsp;* @author salaboy<BR>&nbsp;*/<BR>public class JPAVariablePersisterHibernate implements VariablePersister {<BR>&nbsp;public VariableInstanceInfo persistExternalVariable(String name, Object o,<BR>&nbsp;&nbsp;&nbsp;VariableInstanceInfo oldValue, Environment env)
 {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(o == null || (oldValue != null &amp;&amp; oldValue.getPersister().equals(""))){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;JPAPersistedVariable result = null;<BR>&nbsp;&nbsp;&nbsp;if (oldValue instanceof JPAPersistedVariable) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;result = (JPAPersistedVariable) oldValue;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;if (result == null) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;result = new JPAPersistedVariable();<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;Long idValue = geHibIdValue(o);<BR>&nbsp;&nbsp;&nbsp;result.setPersister(this.getClass().getName());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 result.setName(name);<BR>&nbsp;&nbsp;&nbsp;result.setEntityId(idValue);<BR>&nbsp;&nbsp;&nbsp;result.setEntity(o);<BR>&nbsp;&nbsp;&nbsp;result.setEntityClass(o.getClass().getCanonicalName());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result;<BR>&nbsp;&nbsp;} catch (Throwable t) {<BR>&nbsp;&nbsp;&nbsp;Logger.getLogger(JPAVariablePersisterHibernate.class.getName())<BR>&nbsp;&nbsp;&nbsp;&nbsp;.log(Level.SEVERE, null, t);<BR>&nbsp;&nbsp;&nbsp;throw new RuntimeException("Could not persist external variable", t);<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>&nbsp;public Object getExternalPersistedVariable(<BR>&nbsp;&nbsp;&nbsp;VariableInstanceInfo variableInstanceInfo, Environment env) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(((JPAPersistedVariable) variableInstanceInfo) == null || ((JPAPersistedVariable) variableInstanceInfo).getEntityId() == null){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return
 null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Restoring JPAPersistedVariable id=" + ((JPAPersistedVariable) variableInstanceInfo).getId() + " entityId=" + ((JPAPersistedVariable) variableInstanceInfo).getEntityId() + " class=" + ((JPAPersistedVariable) variableInstanceInfo).getEntityClass() + " value=" + ((JPAPersistedVariable) variableInstanceInfo).getEntity());<BR>&nbsp;&nbsp;String varType = ((JPAPersistedVariable) variableInstanceInfo).getEntityClass();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object obj = HibUtil.getCurrentSession().get(varType, ((JPAPersistedVariable) variableInstanceInfo).getEntityId());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HibUtil.closeCurrentSession();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return obj;<BR>&nbsp;}<BR>/**<BR>&nbsp;* Hibernate lookup to get ID column.<BR>&nbsp;* @param o Dto
 to look up.<BR>&nbsp;* @return Long id.<BR>&nbsp;*/<BR>&nbsp;private Long geHibIdValue(Object o)&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session session = HibUtil.getCurrentSession();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (Long) session.getIdentifier(o);<BR>&nbsp;}<BR>}&nbsp;<BR><BR><BR><BR><BR>_______________________________________________<BR>rules-users mailing list<BR><A href="mailto:rules-users@lists.jboss.org" rel=nofollow target=_blank ymailto="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</A><BR><A href="https://lists.jboss.org/mailman/listinfo/rules-users" rel=nofollow target=_blank>https://lists.jboss.org/mailman/listinfo/rules-users</A><BR></BLOCKQUOTE></DIV><BR><BR clear=all><BR>-- <BR>- http://salaboy.wordpress.com<BR>- http://www.jbug.com.ar<BR>- Salatino "Salaboy" Mauricio -<BR></DIV></DIV></div><br>

      </body></html>