1 package org.hibernate.ejb.callback; 3 4 import org.hibernate.EntityMode; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.engine.EntityEntry; 7 import org.hibernate.engine.SessionImplementor; 8 import org.hibernate.engine.Status; 9 import org.hibernate.event.def.DefaultFlushEntityEventListener; 10 import org.hibernate.metadata.ClassMetadata; 11 import org.hibernate.persister.entity.EntityPersister; 12 import org.hibernate.type.Type; 13 14 19 public class EJB3FlushEntityEventListener extends DefaultFlushEntityEventListener { 20 private EntityCallbackHandler callbackHandler; 21 22 public EJB3FlushEntityEventListener(EntityCallbackHandler callbackHandler) { 23 super(); 24 this.callbackHandler = callbackHandler; 25 } 26 27 @Override protected boolean invokeInterceptor( 28 SessionImplementor session, Object entity, EntityEntry entry, Object [] values, EntityPersister persister 29 ) { 30 boolean isDirty = false; 31 if ( entry.getStatus() != Status.DELETED ) { 32 if ( callbackHandler.preUpdate( entity ) ) { 33 isDirty = copyState( entity, persister.getPropertyTypes(), values, session.getFactory() ); 34 } 35 } 36 return super.invokeInterceptor( session, entity, entry, values, persister ) || isDirty; 37 } 38 39 42 private boolean copyState(Object entity, Type[] types, Object [] state, SessionFactory sf) { 43 ClassMetadata metadata = sf.getClassMetadata( entity.getClass() ); 44 Object [] newState = metadata.getPropertyValues( entity, EntityMode.POJO ); 45 int size = newState.length; 46 boolean isDirty = false; 47 for ( int index = 0 ; index < size ; index++ ) { 48 if ( !types[index].isEqual( state[index], newState[index], EntityMode.POJO ) ) { 49 isDirty = true; 50 state[index] = newState[index]; 51 } 52 } 53 return isDirty; 54 } 55 } 56 | Popular Tags |