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

Bill Tarr javatestcase at yahoo.com
Fri Oct 23 20:14:30 EDT 2009


I'm not sure this should be comitted to the trunk.

Users can't use this code as is, because:

1. Manual/Automatic flush 
   - they'd need to implement flush() and persist() if they have manual flush set up.
2. HibUtil 
   - is a system specific implementation used to manage SessionFactory and Sessions
   - systems using Hibernate will already have some version of this they will need to leverage

so this is more of a template.  

In fact, it won't compile without a HibUtil, so that's another problem for adding it to the trunk...

A test case in examples would let us create a simple HibUtil and hibernate.cfg.xml.

The test case could create a H2 DB and the tables needed...




________________________________
From: Mauricio Salatino <salaboy at gmail.com>
To: Rules Users List <rules-users at lists.jboss.org>
Sent: Fri, October 23, 2009 4:18:16 PM
Subject: Re: [rules-users] [droolsflow] - one way to use Hibernate mapping with JPAVariablePersister

nice.. do you mind if I commit it to the trunk?
can you create some unit testing too?



On Fri, Oct 23, 2009 at 3:27 PM, Bill Tarr <javatestcase at yahoo.com> wrote:

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);
> }
>>
>
>
>
>_______________________________________________
>rules-users mailing list
>rules-users at lists.jboss.org
>https://lists.jboss.org/mailman/listinfo/rules-users
>


-- 
- http://salaboy.wordpress.com
- http://www.jbug.com.ar
- Salatino "Salaboy" Mauricio -



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20091023/2e4eb48c/attachment.html 


More information about the rules-users mailing list