diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/NonNullableTransientDependencies.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/NonNullableTransientDependencies.java index 07fd256e16..8ad404229e 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/NonNullableTransientDependencies.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/NonNullableTransientDependencies.java @@ -27,6 +27,7 @@ import java.util.HashSet; import java.util.IdentityHashMap; import java.util.Map; import java.util.Set; +import java.util.Collections; import org.hibernate.engine.spi.SessionImplementor; @@ -35,12 +36,15 @@ import org.hibernate.engine.spi.SessionImplementor; * * @author Gail Badner */ -public class NonNullableTransientDependencies { +public final class NonNullableTransientDependencies { // Multiple property paths can refer to the same transient entity, so use Set // for the map value. - private final Map> propertyPathsByTransientEntity = new IdentityHashMap>(); + private Map> propertyPathsByTransientEntity; // lazily initialized void add(String propertyName, Object transientEntity) { + if ( propertyPathsByTransientEntity == null ) { + propertyPathsByTransientEntity = new IdentityHashMap>(); + } Set propertyPaths = propertyPathsByTransientEntity.get( transientEntity ); if ( propertyPaths == null ) { propertyPaths = new HashSet(); @@ -50,7 +54,12 @@ public class NonNullableTransientDependencies { } public Iterable getNonNullableTransientEntities() { - return propertyPathsByTransientEntity.keySet(); + if ( propertyPathsByTransientEntity == null ) { + return Collections.emptyList(); + } + else { + return propertyPathsByTransientEntity.keySet(); + } } /** @@ -60,8 +69,13 @@ public class NonNullableTransientDependencies { * * @return The property paths */ - public Iterable getNonNullableTransientPropertyPaths(Object entity) { - return propertyPathsByTransientEntity.get( entity ); + public Iterable getNonNullableTransientPropertyPaths(final Object entity) { + if ( propertyPathsByTransientEntity == null ) { + return Collections.emptyList(); + } + else { + return propertyPathsByTransientEntity.get( entity ); + } } /** @@ -70,7 +84,7 @@ public class NonNullableTransientDependencies { * @return {@code true} indicates there are no path tracked here currently */ public boolean isEmpty() { - return propertyPathsByTransientEntity.isEmpty(); + return propertyPathsByTransientEntity == null || propertyPathsByTransientEntity.isEmpty(); } /** @@ -81,7 +95,7 @@ public class NonNullableTransientDependencies { * @throws IllegalStateException If the entity had tracked paths */ public void resolveNonNullableTransientEntity(Object entity) { - if ( propertyPathsByTransientEntity.remove( entity ) == null ) { + if ( propertyPathsByTransientEntity != null && propertyPathsByTransientEntity.remove( entity ) == null ) { throw new IllegalStateException( "Attempt to resolve a non-nullable, transient entity that is not a dependency." ); } } @@ -95,9 +109,11 @@ public class NonNullableTransientDependencies { */ public String toLoggableString(SessionImplementor session) { final StringBuilder sb = new StringBuilder( getClass().getSimpleName() ).append( '[' ); - for ( Map.Entry> entry : propertyPathsByTransientEntity.entrySet() ) { - sb.append( "transientEntityName=" ).append( session.bestGuessEntityName( entry.getKey() ) ); - sb.append( " requiredBy=" ).append( entry.getValue() ); + if ( propertyPathsByTransientEntity != null ) { + for ( Map.Entry> entry : propertyPathsByTransientEntity.entrySet() ) { + sb.append( "transientEntityName=" ).append( session.bestGuessEntityName( entry.getKey() ) ); + sb.append( " requiredBy=" ).append( entry.getValue() ); + } } sb.append( ']' ); return sb.toString();