You could use an "entityInterceptor" to detect when an entity is changed.
Here's an example:

   <bean id="sessionFactory" class="
org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <property name="mappingResources">
           <list>
               <value>org/appfuse/model/User.hbm.xml</value>
           </list>
       </property>
       <property name="entityInterceptor">
           <bean class="org.appfuse.audit.hibernate.EntityInterceptor"/>
       </property>
       <property name="hibernateProperties">
           <props>
               <prop key="hibernate.dialect">
org.hibernate.dialect.PostgreSQLDialect</prop>
               <prop key="hibernate.hbm2ddl.auto">update</prop>
           </props>
       </property>
   </bean>

EntityInterceptor.java:

package org.appfuse.audit.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.audit.context.AuditContextHolder;
import org.appfuse.audit.context.AuditContext;
import org.appfuse.audit.model.AuditDetail;
import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.Interceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;

import java.io.Serializable;
import java.util.Iterator;

public class EntityInterceptor implements Interceptor, Serializable {
   private final Log log = LogFactory.getLog(EntityInterceptor.class);

   public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
           throws CallbackException {
       try {
           if (entity != null) {
               if (log.isDebugEnabled()) {
                   log.debug(new StringBuffer().append("onSave -->
").append(entity.getClass().getSimpleName()).append(
                           " object"));
               }

           }
       } catch (Exception ex) {
           throw new CallbackException(ex);
       }

       return false;
   }

   public boolean onLoad(Object object, Serializable serializable, Object[]
objects, String[] strings, Type[] types) throws CallbackException {
       if (log.isDebugEnabled()) {
           log.debug(new StringBuffer().append("onLoad --> ").append(
object.getClass().getSimpleName()).append(" object"));
       }
       return false;
   }

   public void onDelete(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
           throws CallbackException {

       try {
           if (log.isDebugEnabled()) {
               log.debug(new StringBuffer().append("onDelete --> ").append(
entity.getClass().getSimpleName()).append(
                       " object"));
           }

       } catch (Exception ex) {
           throw new CallbackException(ex);
       }
   }

   // Didn't bother implementing the methods below

   public boolean onFlushDirty(Object object, Serializable serializable,
Object[] objects, Object[] objects1, String[] strings, Type[] types) throws
CallbackException {
       return false;
   }

   public void onCollectionRecreate(Object object, Serializable
serializable) throws CallbackException {}

   public void onCollectionRemove(Object object, Serializable serializable)
throws CallbackException {}

   public void onCollectionUpdate(Object object, Serializable serializable)
throws CallbackException {}

   public void preFlush(Iterator iterator) throws CallbackException {}

   public void postFlush(Iterator iterator) throws CallbackException {}

   public Boolean isTransient(Object object) {
       return null;
   }

   public int[] findDirty(Object object, Serializable serializable,
Object[] objects, Object[] objects1, String[] strings, Type[] types) {
       return new int[0];
   }

   public Object instantiate(String string, EntityMode entityMode,
Serializable serializable) throws CallbackException {
       return null;
   }

   public String getEntityName(Object object) throws CallbackException {
       return null;
   }

   public Object getEntity(String string, Serializable serializable) throws
CallbackException {
       return null;
   }

   public void afterTransactionBegin(Transaction transaction) {}

   public void beforeTransactionCompletion(Transaction transaction) {}

   public void afterTransactionCompletion(Transaction transaction) {}

   public String onPrepareStatement(String string) {
       return string;
   }
}



On 12/27/06, Gary White <[EMAIL PROTECTED]> wrote:


Given the following:

- AppFuse 1.9.3 (Spring 1.2.8/Spring MVC/Hibernate 3.1.3)
- A QuestionMaster class containing a List of Question objects with the
List
index field/column "revision" (bidirectional parent/child)
- A Survey class with a List of Question objects with the List index
field/column "questionNumber" (unidirectional with join table)

When editing questions, I want to detect if/when  changes to a Question
are
made and create a new revision (i.e. new Question object that is then
added
to the QuestionMaster Question List) and replace the corresponding Survey
Question with the new revision.

I was thinking that I could create a method in the manager/service class
that tests Session.isDirty() to determine if changes were made and then
create a new Question/add it to the QuestionMaster List/etc.  The problem
with this strategy is that I would need to cancel the changes to the
original Question and I'm not sure how to do that.

Any advice/suggestions?

Thanks,
Gary

--
View this message in context:
http://www.nabble.com/creating-object-revisions-tf2888840s2369.html#a8070751
Sent from the AppFuse - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




--
http://raibledesigns.com

Reply via email to