<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> </DIV>
<DIV>Users can't use this code as is, because:</DIV>
<DIV> </DIV>
<DIV>1. Manual/Automatic flush </DIV>
<DIV> - they'd need to implement flush() and persist() if they have manual flush set up.</DIV>
<DIV>2. HibUtil </DIV>
<DIV> - is a system specific implementation used to manage SessionFactory and Sessions</DIV>
<DIV> - systems using Hibernate will already have some version of this they will need to leverage</DIV>
<DIV> </DIV>
<DIV>so this is more of a template. </DIV>
<DIV> </DIV>
<DIV>In fact, it won't compile without a HibUtil, so that's another problem for adding it to the trunk...</DIV>
<DIV> </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> </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 <salaboy@gmail.com><BR><B><SPAN style="FONT-WEIGHT: bold">To:</SPAN></B> Rules Users List <rules-users@lists.jboss.org><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><<A href="mailto:javatestcase@yahoo.com" rel=nofollow target=_blank ymailto="mailto:javatestcase@yahoo.com">javatestcase@yahoo.com</A>></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 mapping files. <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. <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> * Variation on Drools JPA Variable Persister.<BR> * org.drools.persistence.processinstance.persisters.JPAVariablePersister<BR> * for using variables persisted by Hibernate, while still using JPA to<BR> * persist DroolsFlow data.<BR> *<BR> * @author <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>">Kris Verlaenen</a><BR> * @author salaboy<BR> */<BR>public class JPAVariablePersisterHibernate implements VariablePersister {<BR> public VariableInstanceInfo persistExternalVariable(String name, Object o,<BR> VariableInstanceInfo oldValue, Environment env)
{<BR> if(o == null || (oldValue != null && oldValue.getPersister().equals(""))){<BR> return null;<BR> }<BR> try {<BR> JPAPersistedVariable result = null;<BR> if (oldValue instanceof JPAPersistedVariable) {<BR> result = (JPAPersistedVariable) oldValue;<BR> }<BR> if (result == null) {<BR> result = new JPAPersistedVariable();<BR> }<BR> Long idValue = geHibIdValue(o);<BR> result.setPersister(this.getClass().getName());<BR>
result.setName(name);<BR> result.setEntityId(idValue);<BR> result.setEntity(o);<BR> result.setEntityClass(o.getClass().getCanonicalName());<BR> return result;<BR> } catch (Throwable t) {<BR> Logger.getLogger(JPAVariablePersisterHibernate.class.getName())<BR> .log(Level.SEVERE, null, t);<BR> throw new RuntimeException("Could not persist external variable", t);<BR> }<BR> }<BR> public Object getExternalPersistedVariable(<BR> VariableInstanceInfo variableInstanceInfo, Environment env) {<BR> if(((JPAPersistedVariable) variableInstanceInfo) == null || ((JPAPersistedVariable) variableInstanceInfo).getEntityId() == null){<BR> return
null;<BR> }<BR> System.out.println("Restoring JPAPersistedVariable id=" + ((JPAPersistedVariable) variableInstanceInfo).getId() + " entityId=" + ((JPAPersistedVariable) variableInstanceInfo).getEntityId() + " class=" + ((JPAPersistedVariable) variableInstanceInfo).getEntityClass() + " value=" + ((JPAPersistedVariable) variableInstanceInfo).getEntity());<BR> String varType = ((JPAPersistedVariable) variableInstanceInfo).getEntityClass();<BR> <BR> Object obj = HibUtil.getCurrentSession().get(varType, ((JPAPersistedVariable) variableInstanceInfo).getEntityId());<BR> HibUtil.closeCurrentSession();<BR> return obj;<BR> }<BR>/**<BR> * Hibernate lookup to get ID column.<BR> * @param o Dto
to look up.<BR> * @return Long id.<BR> */<BR> private Long geHibIdValue(Object o) {<BR> Session session = HibUtil.getCurrentSession();<BR> return (Long) session.getIdentifier(o);<BR> }<BR>} <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>