From babeacefb0860fa7a04de5bb78700bb109421850 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Wed, 29 Jun 2011 17:56:42 +0800 Subject: [PATCH] HHH-6379 create EntityState enum to replace int constants in AbstractSaveEventListener --- .../internal/AbstractSaveEventListener.java | 18 ++++++++---------- .../internal/DefaultMergeEventListener.java | 6 +++--- .../internal/DefaultPersistEventListener.java | 4 ++-- .../DefaultSaveOrUpdateEventListener.java | 2 +- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractSaveEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractSaveEventListener.java index 7ee4006c85..414fd2e816 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractSaveEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractSaveEventListener.java @@ -59,11 +59,9 @@ import org.hibernate.type.TypeHelper; * @author Steve Ebersole. */ public abstract class AbstractSaveEventListener extends AbstractReassociateEventListener { - - protected static final int PERSISTENT = 0; - protected static final int TRANSIENT = 1; - protected static final int DETACHED = 2; - protected static final int DELETED = 3; + public enum EntityState{ + PERSISTENT, TRANSIENT, DETACHED, DELETED; + } private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, AbstractSaveEventListener.class.getName()); @@ -475,7 +473,7 @@ public abstract class AbstractSaveEventListener extends AbstractReassociateEvent * * @return The state. */ - protected int getEntityState( + protected EntityState getEntityState( Object entity, String entityName, 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 ) { // do nothing for persistent instances if (LOG.isTraceEnabled()) LOG.trace("Persistent instance of: " + getLoggableName(entityName, entity)); - return PERSISTENT; + return EntityState.PERSISTENT; } // ie. e.status==DELETED if (LOG.isTraceEnabled()) LOG.trace("Deleted instance of: " + getLoggableName(entityName, entity)); - return DELETED; + return EntityState.DELETED; } // the object is transient or detached @@ -501,10 +499,10 @@ public abstract class AbstractSaveEventListener extends AbstractReassociateEvent if ( ForeignKeys.isTransient( entityName, entity, getAssumedUnsaved(), source )) { 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)); - return DETACHED; + return EntityState.DETACHED; } protected String getLoggableName(String entityName, Object entity) { diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java index 5dd15a57e7..fca17cd171 100755 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java @@ -219,7 +219,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme copyCache.setOperatedOn( entity, true ); } event.setEntity( entity ); - int entityState = -1; + EntityState entityState = null; // Check the persistence context for an entry relating to this // entity to be merged... @@ -237,12 +237,12 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme // have an incoming entity instance which has a corresponding // entry in the current persistence context, but registered // under a different entity instance - entityState = DETACHED; + entityState = EntityState.DETACHED; } } } - if ( entityState == -1 ) { + if ( entityState == null ) { entityState = getEntityState( entity, event.getEntityName(), entry, source ); } diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistEventListener.java index ff2527790a..d557f88147 100755 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistEventListener.java @@ -102,8 +102,8 @@ public class DefaultPersistEventListener extends AbstractSaveEventListener imple } final EntityEntry entityEntry = source.getPersistenceContext().getEntry( entity ); - int entityState = getEntityState( entity, entityName, entityEntry, source ); - if ( entityState == DETACHED ) { + EntityState entityState = getEntityState( entity, entityName, entityEntry, source ); + if ( entityState == EntityState.DETACHED ) { // 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. // The issue is that this causes problems with the Hibernate unsaved-value strategy diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultSaveOrUpdateEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultSaveOrUpdateEventListener.java index 4e648f8005..a59d7fdebe 100755 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultSaveOrUpdateEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultSaveOrUpdateEventListener.java @@ -96,7 +96,7 @@ public class DefaultSaveOrUpdateEventListener extends AbstractSaveEventListener } protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) { - int entityState = getEntityState( + EntityState entityState = getEntityState( event.getEntity(), event.getEntityName(), event.getEntry(),