[rules-users] [droolsflow] - one way to use Hibernate mapping with JPAVariablePersister

Bill Tarr javatestcase at yahoo.com
Fri Oct 23 18:27:43 EDT 2009


Here is an alternate version of JPAVariablePersister for people who are already using Hibernate xml mapping files.  

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.  

We are also currently using automatic flush (so no code to persist domain objects 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.JPAPersistedVariable;
import org.drools.persistence.processinstance.variabletypes.VariableInstanceInfo;
import org.drools.persistence.processinstance.persisters.VariablePersister;
import org.drools.runtime.Environment;
import org.hibernate.Session;
import HibUtil;
/**
 * Variation on Drools JPA Variable Persister.
 * org.drools.persistence.processinstance.persisters.JPAVariablePersister
 * for using variables persisted by Hibernate, while still using JPA to
 * persist DroolsFlow data.
 *
 * @author <a href="mailto:kris_verlaenen at hotmail.com">Kris Verlaenen</a>
 * @author salaboy
 */
public class JPAVariablePersisterHibernate implements VariablePersister {
 public VariableInstanceInfo persistExternalVariable(String name, Object o,
   VariableInstanceInfo oldValue, Environment env) {
                if(o == null || (oldValue != null && oldValue.getPersister().equals(""))){
                    return null;
                }
  try {
   JPAPersistedVariable result = null;
   if (oldValue instanceof JPAPersistedVariable) {
    result = (JPAPersistedVariable) oldValue;
   }
   if (result == null) {
    result = new JPAPersistedVariable();
   }
   Long idValue = geHibIdValue(o);
   result.setPersister(this.getClass().getName());
            result.setName(name);
   result.setEntityId(idValue);
   result.setEntity(o);
   result.setEntityClass(o.getClass().getCanonicalName());
            return result;
  } catch (Throwable t) {
   Logger.getLogger(JPAVariablePersisterHibernate.class.getName())
    .log(Level.SEVERE, null, t);
   throw new RuntimeException("Could not persist external variable", t);
  }
 }
 public Object getExternalPersistedVariable(
   VariableInstanceInfo variableInstanceInfo, Environment env) {
        if(((JPAPersistedVariable) variableInstanceInfo) == null || ((JPAPersistedVariable) variableInstanceInfo).getEntityId() == null){
            return null;
        }
        System.out.println("Restoring JPAPersistedVariable id=" + ((JPAPersistedVariable) variableInstanceInfo).getId() + " entityId=" + ((JPAPersistedVariable) variableInstanceInfo).getEntityId() + " class=" + ((JPAPersistedVariable) variableInstanceInfo).getEntityClass() + " value=" + ((JPAPersistedVariable) variableInstanceInfo).getEntity());
  String varType = ((JPAPersistedVariable) variableInstanceInfo).getEntityClass();
        
        Object obj = HibUtil.getCurrentSession().get(varType, ((JPAPersistedVariable) variableInstanceInfo).getEntityId());
        HibUtil.closeCurrentSession();
        return obj;
 }
/**
 * Hibernate lookup to get ID column.
 * @param o Dto to look up.
 * @return Long id.
 */
 private Long geHibIdValue(Object o)  {
        Session session = HibUtil.getCurrentSession();
        return (Long) session.getIdentifier(o);
 }
} 


      




More information about the rules-users mailing list