From javatestcase at yahoo.com Fri Oct 23 18:27:44 2009 Content-Type: multipart/mixed; boundary="===============3705900764124495606==" MIME-Version: 1.0 From: Bill Tarr To: rules-users at lists.jboss.org Subject: [rules-users] [droolsflow] - one way to use Hibernate mapping with JPAVariablePersister Date: Fri, 23 Oct 2009 15:27:43 -0700 Message-ID: <635409.3225.qm@web45911.mail.sp1.yahoo.com> --===============3705900764124495606== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Here is an alternate version of JPAVariablePersister for people who are alr= eady using Hibernate xml=C2=A0mapping files.=C2=A0 = This implementation has some downsides, but it seems to be working for now. I am going to keep using JPA to manage all the Drools Flow tables. My domain object persistence will be managed by Hibernate Session.=C2=A0 = We are also currently using automatic flush (so no code to persist domain o= bjects in this class) Obviously you need to use your own version of HibUtil. import java.util.logging.Level; import java.util.logging.Logger; import org.drools.persistence.processinstance.variabletypes.JPAPersistedVar= iable; import org.drools.persistence.processinstance.variabletypes.VariableInstanc= eInfo; import org.drools.persistence.processinstance.persisters.VariablePersister; import org.drools.runtime.Environment; import org.hibernate.Session; import HibUtil; /** =C2=A0* Variation on Drools JPA Variable Persister. =C2=A0* org.drools.persistence.processinstance.persisters.JPAVariablePersis= ter =C2=A0* for using variables persisted by Hibernate, while still using JPA to =C2=A0* persist DroolsFlow data. =C2=A0* =C2=A0* @author Kris Verlae= nen =C2=A0* @author salaboy =C2=A0*/ public class JPAVariablePersisterHibernate implements VariablePersister { =C2=A0public VariableInstanceInfo persistExternalVariable(String name, Obje= ct o, =C2=A0=C2=A0=C2=A0VariableInstanceInfo oldValue, Environment env) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 if(o =3D=3D null || (oldValue !=3D null && oldValue.getPers= ister().equals(""))){ =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return null; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 } =C2=A0=C2=A0try { =C2=A0=C2=A0=C2=A0JPAPersistedVariable result =3D null; =C2=A0=C2=A0=C2=A0if (oldValue instanceof JPAPersistedVariable) { =C2=A0=C2=A0=C2=A0=C2=A0result =3D (JPAPersistedVariable) oldValue; =C2=A0=C2=A0=C2=A0} =C2=A0=C2=A0=C2=A0if (result =3D=3D null) { =C2=A0=C2=A0=C2=A0=C2=A0result =3D new JPAPersistedVariable(); =C2=A0=C2=A0=C2=A0} =C2=A0=C2=A0=C2=A0Long idValue =3D geHibIdValue(o); =C2=A0=C2=A0=C2=A0result.setPersister(this.getClass().getName()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 result.s= etName(name); =C2=A0=C2=A0=C2=A0result.setEntityId(idValue); =C2=A0=C2=A0=C2=A0result.setEntity(o); =C2=A0=C2=A0=C2=A0result.setEntityClass(o.getClass().getCanonicalName()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return r= esult; =C2=A0=C2=A0} catch (Throwable t) { =C2=A0=C2=A0=C2=A0Logger.getLogger(JPAVariablePersisterHibernate.class.getN= ame()) =C2=A0=C2=A0=C2=A0=C2=A0.log(Level.SEVERE, null, t); =C2=A0=C2=A0=C2=A0throw new RuntimeException("Could not persist external va= riable", t); =C2=A0=C2=A0} =C2=A0} =C2=A0public Object getExternalPersistedVariable( =C2=A0=C2=A0=C2=A0VariableInstanceInfo variableInstanceInfo, Environment en= v) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if(((JPAPersistedVariable) varia= bleInstanceInfo) =3D=3D null || ((JPAPersistedVariable) variableInstanceInf= o).getEntityId() =3D=3D null){ =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return n= ull; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 System.out.println("Restoring JP= APersistedVariable id=3D" + ((JPAPersistedVariable) variableInstanceInfo).g= etId() + " entityId=3D" + ((JPAPersistedVariable) variableInstanceInfo).get= EntityId() + " class=3D" + ((JPAPersistedVariable) variableInstanceInfo).ge= tEntityClass() + " value=3D" + ((JPAPersistedVariable) variableInstanceInfo= ).getEntity()); =C2=A0=C2=A0String varType =3D ((JPAPersistedVariable) variableInstanceInfo= ).getEntityClass(); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Object obj =3D HibUtil.getCurren= tSession().get(varType, ((JPAPersistedVariable) variableInstanceInfo).getEn= tityId()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 HibUtil.closeCurrentSession(); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return obj; =C2=A0} /** =C2=A0* Hibernate lookup to get ID column. =C2=A0* @param o Dto to look up. =C2=A0* @return Long id. =C2=A0*/ =C2=A0private Long geHibIdValue(Object o)=C2=A0 { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Session session =3D HibUtil.getC= urrentSession(); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return (Long) session.getIdentif= ier(o); =C2=A0} }=C2=A0 = --===============3705900764124495606==--