HHH-6379 create EntityState enum to replace int constants in AbstractSaveEventListener

This commit is contained in:
Strong Liu 2011-06-29 17:56:42 +08:00
parent 0c757327bd
commit babeacefb0
4 changed files with 14 additions and 16 deletions

View File

@ -59,11 +59,9 @@ import org.hibernate.type.TypeHelper;
* @author Steve Ebersole. * @author Steve Ebersole.
*/ */
public abstract class AbstractSaveEventListener extends AbstractReassociateEventListener { public abstract class AbstractSaveEventListener extends AbstractReassociateEventListener {
public enum EntityState{
protected static final int PERSISTENT = 0; PERSISTENT, TRANSIENT, DETACHED, DELETED;
protected static final int TRANSIENT = 1; }
protected static final int DETACHED = 2;
protected static final int DELETED = 3;
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
AbstractSaveEventListener.class.getName()); AbstractSaveEventListener.class.getName());
@ -475,7 +473,7 @@ public abstract class AbstractSaveEventListener extends AbstractReassociateEvent
* *
* @return The state. * @return The state.
*/ */
protected int getEntityState( protected EntityState getEntityState(
Object entity, Object entity,
String entityName, String entityName,
EntityEntry entry, //pass this as an argument only to avoid double looking EntityEntry entry, //pass this as an argument only to avoid double looking
@ -487,11 +485,11 @@ public abstract class AbstractSaveEventListener extends AbstractReassociateEvent
if ( entry.getStatus() != Status.DELETED ) { if ( entry.getStatus() != Status.DELETED ) {
// do nothing for persistent instances // do nothing for persistent instances
if (LOG.isTraceEnabled()) LOG.trace("Persistent instance of: " + getLoggableName(entityName, entity)); if (LOG.isTraceEnabled()) LOG.trace("Persistent instance of: " + getLoggableName(entityName, entity));
return PERSISTENT; return EntityState.PERSISTENT;
} }
// ie. e.status==DELETED // ie. e.status==DELETED
if (LOG.isTraceEnabled()) LOG.trace("Deleted instance of: " + getLoggableName(entityName, entity)); if (LOG.isTraceEnabled()) LOG.trace("Deleted instance of: " + getLoggableName(entityName, entity));
return DELETED; return EntityState.DELETED;
} }
// the object is transient or detached // the object is transient or detached
@ -501,10 +499,10 @@ public abstract class AbstractSaveEventListener extends AbstractReassociateEvent
if ( ForeignKeys.isTransient( entityName, entity, getAssumedUnsaved(), source )) { if ( ForeignKeys.isTransient( entityName, entity, getAssumedUnsaved(), source )) {
if (LOG.isTraceEnabled()) LOG.trace("Transient instance of: " + getLoggableName(entityName, entity)); if (LOG.isTraceEnabled()) LOG.trace("Transient instance of: " + getLoggableName(entityName, entity));
return TRANSIENT; return EntityState.TRANSIENT;
} }
if (LOG.isTraceEnabled()) LOG.trace("Detached instance of: " + getLoggableName(entityName, entity)); if (LOG.isTraceEnabled()) LOG.trace("Detached instance of: " + getLoggableName(entityName, entity));
return DETACHED; return EntityState.DETACHED;
} }
protected String getLoggableName(String entityName, Object entity) { protected String getLoggableName(String entityName, Object entity) {

View File

@ -219,7 +219,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
copyCache.setOperatedOn( entity, true ); copyCache.setOperatedOn( entity, true );
} }
event.setEntity( entity ); event.setEntity( entity );
int entityState = -1; EntityState entityState = null;
// Check the persistence context for an entry relating to this // Check the persistence context for an entry relating to this
// entity to be merged... // entity to be merged...
@ -237,12 +237,12 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
// have an incoming entity instance which has a corresponding // have an incoming entity instance which has a corresponding
// entry in the current persistence context, but registered // entry in the current persistence context, but registered
// under a different entity instance // under a different entity instance
entityState = DETACHED; entityState = EntityState.DETACHED;
} }
} }
} }
if ( entityState == -1 ) { if ( entityState == null ) {
entityState = getEntityState( entity, event.getEntityName(), entry, source ); entityState = getEntityState( entity, event.getEntityName(), entry, source );
} }

View File

@ -102,8 +102,8 @@ public class DefaultPersistEventListener extends AbstractSaveEventListener imple
} }
final EntityEntry entityEntry = source.getPersistenceContext().getEntry( entity ); final EntityEntry entityEntry = source.getPersistenceContext().getEntry( entity );
int entityState = getEntityState( entity, entityName, entityEntry, source ); EntityState entityState = getEntityState( entity, entityName, entityEntry, source );
if ( entityState == DETACHED ) { if ( entityState == EntityState.DETACHED ) {
// JPA 2, in its version of a "foreign generated", allows the id attribute value // JPA 2, in its version of a "foreign generated", allows the id attribute value
// to be manually set by the user, even though this manual value is irrelevant. // to be manually set by the user, even though this manual value is irrelevant.
// The issue is that this causes problems with the Hibernate unsaved-value strategy // The issue is that this causes problems with the Hibernate unsaved-value strategy

View File

@ -96,7 +96,7 @@ public class DefaultSaveOrUpdateEventListener extends AbstractSaveEventListener
} }
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) { protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) {
int entityState = getEntityState( EntityState entityState = getEntityState(
event.getEntity(), event.getEntity(),
event.getEntityName(), event.getEntityName(),
event.getEntry(), event.getEntry(),