diff --git a/hibernate-core/src/main/java/org/hibernate/type/ArrayType.java b/hibernate-core/src/main/java/org/hibernate/type/ArrayType.java index fa2958d318..28083585de 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/ArrayType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/ArrayType.java @@ -11,9 +11,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import org.hibernate.Hibernate; import org.hibernate.HibernateException; -import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer; import org.hibernate.collection.spi.PersistentArrayHolder; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.SessionFactoryImplementor; @@ -21,6 +19,9 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.metamodel.CollectionClassification; import org.hibernate.persister.collection.CollectionPersister; +import static org.hibernate.Hibernate.isInitialized; +import static org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer.UNFETCHED_PROPERTY; + /** * A type for persistent arrays. * @author Gavin King @@ -48,7 +49,7 @@ public class ArrayType extends CollectionType { @Override public PersistentCollection instantiate(SharedSessionContractImplementor session, CollectionPersister persister, Object key) - throws HibernateException { + throws HibernateException { return new PersistentArrayHolder<>(session, persister); } @@ -75,20 +76,21 @@ public class ArrayType extends CollectionType { if ( value == null ) { return "null"; } - int length = Array.getLength(value); - List list = new ArrayList<>(length); - Type elemType = getElementType(factory); - for ( int i=0; i" ); - } - else { - list.add( elemType.toLoggableString( element, factory ) ); + else { + final int length = Array.getLength( value ); + final Type elemType = getElementType( factory ); + final List list = new ArrayList<>( length ); + for ( int i = 0; i < length; i++ ) { + list.add( loggableString( factory, Array.get( value, i ), elemType ) ); } + return list.toString(); } - return list.toString(); + } + + private static String loggableString(SessionFactoryImplementor factory, Object element, Type elemType) { + return element == UNFETCHED_PROPERTY || !isInitialized( element ) + ? "" + : elemType.toLoggableString( element, factory ); } @Override @@ -101,16 +103,16 @@ public class ArrayType extends CollectionType { Object original, Object target, Object owner, - Map copyCache, + Map copyCache, SharedSessionContractImplementor session) throws HibernateException { - int length = Array.getLength(original); + final int length = Array.getLength(original); if ( length!=Array.getLength(target) ) { //note: this affects the return value! target=instantiateResult(original); } - Type elemType = getElementType( session.getFactory() ); + final Type elemType = getElementType( session.getFactory() ); for ( int i=0; i( anticipatedSize + (int) ( anticipatedSize * .75f ), .75f ); } - @Override @SuppressWarnings("rawtypes") + @Override @SuppressWarnings({"rawtypes", "unchecked"}) public Object replaceElements( final Object original, final Object target, final Object owner, - final Map copyCache, + final Map copyCache, final SharedSessionContractImplementor session) throws HibernateException { - CollectionPersister cp = session.getFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor( getRole() ); + final CollectionPersister persister = + session.getFactory().getRuntimeMetamodels().getMappingMetamodel() + .getCollectionDescriptor( getRole() ); - Map result = (Map) target; + final Map source = (Map) original; + final Map result = (Map) target; result.clear(); - for ( Object o : ( (Map) original ).entrySet() ) { - Map.Entry me = (Map.Entry) o; - Object key = cp.getIndexType().replace( me.getKey(), null, session, owner, copyCache ); - Object value = cp.getElementType().replace( me.getValue(), null, session, owner, copyCache ); + for ( Object entry : source.entrySet() ) { + final Map.Entry me = (Map.Entry) entry; + final Object key = persister.getIndexType().replace( me.getKey(), null, session, owner, copyCache ); + final Object value = persister.getElementType().replace( me.getValue(), null, session, owner, copyCache ); result.put( key, value ); } @@ -82,8 +85,9 @@ public class MapType extends CollectionType { @Override @SuppressWarnings("rawtypes") public Object indexOf(Object collection, Object element) { - for ( Object o : ( (Map) collection ).entrySet() ) { - Map.Entry me = (Map.Entry) o; + final Map map = (Map) collection; + for ( Object entry : map.entrySet() ) { + final Map.Entry me = (Map.Entry) entry; //TODO: proxies! if ( me.getValue() == element ) { return me.getKey();